Google sheets script Auto-运行 脚本并在菜单栏添加功能
Google sheets script Auto-run a script and add function in the menu bar
我使用的脚本允许我删除 table 中间的空行,并不断在底部添加行,以便我可以继续填充它。
一切都很完美。
如果我在“C”列中删除或添加一个值,我只希望脚本自动 运行。而且,如果可能的话,在我的菜单栏中使用 onOpen(e) 添加一个功能,以防脚本不执行并且我必须手动 运行 它。
这是我的表格:
这是脚本:
function removeEmpty() {
const sh = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Suivi Clients');
const empty_rows = [];
const lastRow = sh.getLastRow()
const data = sh.getRange("C6:G" + lastRow).getValues();
for (var i in data) if (data[i].join('') == '') empty_rows.push(+i + 6);
empty_rows.reverse().forEach(x => sh.deleteRow(x));
sh.insertRowsAfter(lastRow - empty_rows.length, 5)
var rng = sh.getRange('A6:Z6')
rng.copyTo(sh.getRange('A' + (lastRow - empty_rows.length + 1) + ':Z' + (lastRow - empty_rows.length + 5)), SpreadsheetApp.CopyPasteType.PASTE_FORMAT, false);
// H-I-J-K-L
var rng = sh.getRange('H' + (lastRow - empty_rows.length) + ':L' + (lastRow - empty_rows.length))
rng.copyTo(sh.getRange('H' + (lastRow - empty_rows.length + 1) + ':L' + (lastRow - empty_rows.length + 5)), SpreadsheetApp.CopyPasteType.PASTE_FORMULA, false);
// O
var rng = sh.getRange('O' + (lastRow - empty_rows.length) + ':O' + (lastRow - empty_rows.length))
rng.copyTo(sh.getRange('O' + (lastRow - empty_rows.length + 1) + ':O' + (lastRow - empty_rows.length + 5)), SpreadsheetApp.CopyPasteType.PASTE_FORMULA, false);
// Q-R-S-T-U
var rng = sh.getRange('Q' + (lastRow - empty_rows.length) + ':U' + (lastRow - empty_rows.length))
rng.copyTo(sh.getRange('Q' + (lastRow - empty_rows.length + 1) + ':U' + (lastRow - empty_rows.length + 5)), SpreadsheetApp.CopyPasteType.PASTE_FORMULA, false);
// X-Y
var rng = sh.getRange('X' + (lastRow - empty_rows.length) + ':Y' + (lastRow - empty_rows.length))
rng.copyTo(sh.getRange('X' + (lastRow - empty_rows.length + 1) + ':Y' + (lastRow - empty_rows.length + 5)), SpreadsheetApp.CopyPasteType.PASTE_FORMULA, false);
}
试试这个
function onEdit(e) {
var sh = e.source.getActiveSheet();
if (sh.getName() != 'Suivi Clients') return;
var editRange = {
top: 6,
left: 3,
right: 7
};
var thisRow = e.range.getRow();
if (thisRow < editRange.top || thisRow > editRange.bottom) return;
var thisCol = e.range.getColumn();
if (thisCol < editRange.left || thisCol > editRange.right) return;
removeEmpty()
}
和
function onOpen() {
SpreadsheetApp.getUi().createMenu('⇩ M E N U ⇩')
.addItem(' remove empty lines', 'removeEmpty')
.addToUi();
removeEmpty()
}
我认为你的目标如下:
function onEdit(e) {
const {range} = e
/* Ensure that the edit occurs in the column 3 === C */
if(range.getColumn() === 3 && range.getLastColumn() === 3){
removeEmpty()
}
}
function onOpen(e) {
SpreadsheetApp.getUi().createMenu().addItem('Remove Empty', 'removeEmpty').addToUi()
}
我使用的脚本允许我删除 table 中间的空行,并不断在底部添加行,以便我可以继续填充它。 一切都很完美。 如果我在“C”列中删除或添加一个值,我只希望脚本自动 运行。而且,如果可能的话,在我的菜单栏中使用 onOpen(e) 添加一个功能,以防脚本不执行并且我必须手动 运行 它。 这是我的表格:
这是脚本:
function removeEmpty() {
const sh = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Suivi Clients');
const empty_rows = [];
const lastRow = sh.getLastRow()
const data = sh.getRange("C6:G" + lastRow).getValues();
for (var i in data) if (data[i].join('') == '') empty_rows.push(+i + 6);
empty_rows.reverse().forEach(x => sh.deleteRow(x));
sh.insertRowsAfter(lastRow - empty_rows.length, 5)
var rng = sh.getRange('A6:Z6')
rng.copyTo(sh.getRange('A' + (lastRow - empty_rows.length + 1) + ':Z' + (lastRow - empty_rows.length + 5)), SpreadsheetApp.CopyPasteType.PASTE_FORMAT, false);
// H-I-J-K-L
var rng = sh.getRange('H' + (lastRow - empty_rows.length) + ':L' + (lastRow - empty_rows.length))
rng.copyTo(sh.getRange('H' + (lastRow - empty_rows.length + 1) + ':L' + (lastRow - empty_rows.length + 5)), SpreadsheetApp.CopyPasteType.PASTE_FORMULA, false);
// O
var rng = sh.getRange('O' + (lastRow - empty_rows.length) + ':O' + (lastRow - empty_rows.length))
rng.copyTo(sh.getRange('O' + (lastRow - empty_rows.length + 1) + ':O' + (lastRow - empty_rows.length + 5)), SpreadsheetApp.CopyPasteType.PASTE_FORMULA, false);
// Q-R-S-T-U
var rng = sh.getRange('Q' + (lastRow - empty_rows.length) + ':U' + (lastRow - empty_rows.length))
rng.copyTo(sh.getRange('Q' + (lastRow - empty_rows.length + 1) + ':U' + (lastRow - empty_rows.length + 5)), SpreadsheetApp.CopyPasteType.PASTE_FORMULA, false);
// X-Y
var rng = sh.getRange('X' + (lastRow - empty_rows.length) + ':Y' + (lastRow - empty_rows.length))
rng.copyTo(sh.getRange('X' + (lastRow - empty_rows.length + 1) + ':Y' + (lastRow - empty_rows.length + 5)), SpreadsheetApp.CopyPasteType.PASTE_FORMULA, false);
}
试试这个
function onEdit(e) {
var sh = e.source.getActiveSheet();
if (sh.getName() != 'Suivi Clients') return;
var editRange = {
top: 6,
left: 3,
right: 7
};
var thisRow = e.range.getRow();
if (thisRow < editRange.top || thisRow > editRange.bottom) return;
var thisCol = e.range.getColumn();
if (thisCol < editRange.left || thisCol > editRange.right) return;
removeEmpty()
}
和
function onOpen() {
SpreadsheetApp.getUi().createMenu('⇩ M E N U ⇩')
.addItem(' remove empty lines', 'removeEmpty')
.addToUi();
removeEmpty()
}
我认为你的目标如下:
function onEdit(e) {
const {range} = e
/* Ensure that the edit occurs in the column 3 === C */
if(range.getColumn() === 3 && range.getLastColumn() === 3){
removeEmpty()
}
}
function onOpen(e) {
SpreadsheetApp.getUi().createMenu().addItem('Remove Empty', 'removeEmpty').addToUi()
}