我是Google Scripts的新手,并尝试在Google Spreadsheets中自动执行某些流程。我找到了一种方法,根据特定列中的值将一行从一个工作表移动到另一个工作表。但是,为了使它能够在多个工作表中工作,我已经复制了每张工作表的代码。我确定必须有办法简化这一点,但我会陷入困境。请参阅下面的代码。
function onEdit() {
// moves a row from a sheet to another when a magic value is entered in a column
// adjust the following variables to fit your needs
// see https://productforums.google.com/d/topic/docs/ehoCZjFPBao/discussion
var sheetNameToWatch = "BRG";
var columnNumberToWatch = 3; // column A = 1, B = 2, etc.
var valueToWatch = "done";
var sheetNameToMoveTheRowTo = "Done";
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getActiveCell();
if (sheet.getName() == sheetNameToWatch && range.getColumn() == columnNumberToWatch && range.getValue() == valueToWatch) {
var targetSheet = ss.getSheetByName(sheetNameToMoveTheRowTo);
var targetRange = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
sheet.getRange(range.getRow(), 1, 1, sheet.getLastColumn()).moveTo(targetRange);
sheet.deleteRow(range.getRow());
}
var sheetNameToWatch = "Lesotho";
var columnNumberToWatch = 3; // column A = 1, B = 2, etc.
var valueToWatch = "done";
var sheetNameToMoveTheRowTo = "Done";
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getActiveCell();
if (sheet.getName() == sheetNameToWatch && range.getColumn() == columnNumberToWatch && range.getValue() == valueToWatch) {
var targetSheet = ss.getSheetByName(sheetNameToMoveTheRowTo);
var targetRange = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
sheet.getRange(range.getRow(), 1, 1, sheet.getLastColumn()).moveTo(targetRange);
sheet.deleteRow(range.getRow());
}
var sheetNameToWatch = "Bosman";
var columnNumberToWatch = 3; // column A = 1, B = 2, etc.
var valueToWatch = "done";
var sheetNameToMoveTheRowTo = "Done";
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getActiveCell();
if (sheet.getName() == sheetNameToWatch && range.getColumn() == columnNumberToWatch && range.getValue() == valueToWatch) {
var targetSheet = ss.getSheetByName(sheetNameToMoveTheRowTo);
var targetRange = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
sheet.getRange(range.getRow(), 1, 1, sheet.getLastColumn()).moveTo(targetRange);
sheet.deleteRow(range.getRow());
}
}
答案 0 :(得分:0)
这应该这样做。它创建一个不等于“完成”的所有工作表名称的数组,然后遍历您的代码以查看是否已在C列中使用“完成”编辑了工作表。
function onEdit() {
var columnNumberToWatch = 3; // column A = 1, B = 2, etc.
var valueToWatch = "done";
var sheetNameToMoveTheRowTo = "Done";
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getActiveCell();
var sheetNameToWatch=[] //Array of Sheet names
var names = ss.getSheets()
for( j=0;j<names.length;j++) {
var n= names[j].getSheetName();
if(n!="Done"){ //If Sheet name not "Done" add to array
sheetNameToWatch.push(n)
}}
for(i=0;i<sheetNameToWatch.length;i++){
if (sheet.getName() == sheetNameToWatch[i] && range.getColumn() == columnNumberToWatch && range.getValue() == valueToWatch) {
var targetSheet = ss.getSheetByName(sheetNameToMoveTheRowTo);
var targetRange = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
sheet.getRange(range.getRow(), 1, 1, sheet.getLastColumn()).moveTo(targetRange);
sheet.deleteRow(range.getRow());
}}
}