-
Notifications
You must be signed in to change notification settings - Fork 1
/
Code.gs
110 lines (105 loc) · 3.75 KB
/
Code.gs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/**
* @OnlyCurrentDoc Limits the script to only accessing the current spreadsheet.
*/
/**
* Creates menu entries in the Sheets UI when the document is opened.
*/
function onOpen(e) {
var ui = SpreadsheetApp.getUi();
ui.createMenu('Insert Cells')
.addItem('Shift Down', 'shiftDown')
.addItem('Shift Right', 'shiftRight')
.addToUi();
ui.createMenu('Delete Cells')
.addItem('Shift Up', 'shiftUp')
.addItem('Shift Left', 'shiftLeft')
.addToUi();
}
/**
* Insert blank cells and shift existing content down
*/
function shiftDown() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var cell = sheet.getActiveCell();
var row = cell.getRow();
var col = cell.getColumn();
var range = sheet.getActiveRange();
var numRows = range.getNumRows();
var numCols = range.getNumColumns();
var lastRow = sheet.getLastRow();
try {
sheet.getRange(row, col, lastRow - row + 1, numCols).moveTo(sheet.getRange(row + numRows, col, lastRow - row + 1, numCols));
range.clearContent();
}
catch(err) {
SpreadsheetApp.getUi().alert('Could not move data: Please make sure nobody else is editing the column(s) and try again.');
}
}
/**
* Insert blank cells and shift existing content to the right
*/
function shiftRight() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var cell = sheet.getActiveCell();
var row = cell.getRow();
var col = cell.getColumn();
var range = sheet.getActiveRange();
var numRows = range.getNumRows();
var numCols = range.getNumColumns();
var lastCol = sheet.getLastColumn();
try {
sheet.getRange(row, col, numRows, lastCol - col + 1).moveTo(sheet.getRange(row, col + numCols, numRows, lastCol - col + 1));
range.clearContent();
}
catch (err) {
SpreadsheetApp.getUi().alert('Could not move data: Please make sure nobody else is editing the row(s) and try again.');
}
}
/**
* Delete cells and shift up content from below
*/
function shiftUp() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var cell = sheet.getActiveCell();
var row = cell.getRow();
var col = cell.getColumn();
var range = sheet.getActiveRange();
var numRows = range.getNumRows();
var numCols = range.getNumColumns();
var lastRow = sheet.getLastRow();
try {
sheet.getRange(row + numRows, col, lastRow - row - numRows + 1, numCols).moveTo(sheet.getRange(row, col, lastRow - row - numRows + 1, numCols));
sheet.getRange(lastRow - numRows + 1, col, numRows, numCols).clearContent();
}
catch(err) {
if (row + numRows > lastRow) {
SpreadsheetApp.getUi().alert('Select a different range: Cannot shift up from the last populated row of a sheet.');
} else {
SpreadsheetApp.getUi().alert('Could not move data: Please make sure nobody else is editing the column(s) and try again.');
}
}
}
/**
* Delete cells and shift left content from the right
*/
function shiftLeft() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var cell = sheet.getActiveCell();
var row = cell.getRow();
var col = cell.getColumn();
var range = sheet.getActiveRange();
var numRows = range.getNumRows();
var numCols = range.getNumColumns();
var lastCol = sheet.getLastColumn();
try {
sheet.getRange(row, col + numCols, numRows, lastCol - col - numCols + 1).moveTo(sheet.getRange(row, col, numRows, lastCol - col - numCols + 1));
sheet.getRange(row, lastCol - numCols + 1, numRows, numCols).clearContent();
}
catch(err) {
if (col + numCols > lastCol) {
SpreadsheetApp.getUi().alert('Select a different range: Cannot shift left from the last populated column of a sheet.');
} else {
SpreadsheetApp.getUi().alert('Could not move data: Please make sure nobody else is editing the row(s) and try again.');
}
}
}