我有2张不同的纸张,第一张是存储和更改所有数据的地方。在更新第一张纸时,它将移动到第二张纸。我想弄清楚在更新之后,我可以根据第Q列或第17列中的数据从第二张到第二张中的另一个标签移动一行。这是我目前拥有的,但它没有似乎在移动任何东西。这甚至可能吗?
function onUpdate() {
var sheetNameToWatch = "Raw Data";
var columnNumberToWatch = 17; // column A = 1, B = 2, etc.
var valueToWatch = ["Full", "Partial", "Process", "Conditional", "Fail", "Exemption"];
var sheetNameToMoveTheRowTo = (valueToWatch + " Certification");;
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());
}
}
感谢。
好的,我已经更新了当前的脚本:
function onUpdate() {
var sourceSheet = "Raw Data";
var columnNumberToWatch = 17; // column A = 1, B = 2, etc.
var valueToWatch = ["Full", "Partial", "Process", "Conditional", "Fail", "Exemption"];
var sheetNameToMoveTheRowTo = (valueToWatch + " Certification");
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getActiveCell();
if (sheet.getName() === sourceSheet && range.getColumn() === columnNumberToWatch && checkMatch(range.getValue(), valueToWatch) === true) {
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());
}
}
function checkMatch(value, array) {
for (i in array) {
if (value === array[i]) {
return(true);
}
}
}
好的,
我试着用它来指定一点:
function onEdit() {
var sourceSheet = "Raw Data";
var columnNumberToWatch = 17; // column A = 1, B = 2, etc.
var valueToWatch = ["Full", "Partial", "Process", "Conditional", "Fail", "Exemption"];
var sheetNameToMoveTheRowTo = (valueToWatch + " Certification");
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getActiveCell();
var cert = range.getValue();
if (valueToWatch.indexOf(cert) === sourceSheet && range.getColumn() === columnNumberToWatch && checkMatch(range.getValue(), valueToWatch) === true) {
var sheetNameToMoveTheRowTo = (cert + " Certification");
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());
}
}
function checkMatch(value, array) {
for (i in array) {
if (value === array[i]) {
return(true);
}
}
}
通过定义cert并尝试更清楚地定义valueToWatch的数组。
答案 0 :(得分:0)
您需要更改if语句。它应该是“===”而不是“==”。 Here背后有一个很好的解释。
以下是您可以使用的最终脚本
function onEdit(e) {
var sourceSheet = "Raw Data";
var columnNumberToWatch = 17; // column A = 1, B = 2, etc.
var valueToWatch = ["Full", "Partial", "Process", "Conditional", "Fail", "Exemption"];
var ss = SpreadsheetApp.getActiveSpreadsheet();
var range = e.range;
var activesheet = range.getSheet();
var activesheetname = activesheet.getName();
var row = range.getRow();
var value = ss.getSheetByName(sourceSheet).getRange(row, columnNumberToWatch).getValue();
if (activesheetname === sourceSheet && valueToWatch.indexOf(value) >=0) {
var targetSheet = ss.getSheetByName(value + " Certification");
var targetRange = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
activesheet.getRange(row, 1, 1, activesheet.getLastColumn()).moveTo(targetRange);
activesheet.deleteRow(row);
}
}
<强>更新强> 如果不手动编辑单元格,则无法在第二个电子表格上运行脚本。一旦在第二个电子表格中从第一个电子表格推送该行,它将不会在第二个电子表格中触发onEdit函数。您可以做的是将其设置为从第一个电子表格中将行分配到正确的表格,如下所示
//This is part of a script on the first spreadsheet
if (valueToWatch.indexOf(value) >=0) {
var targetSheetName = value + " Certification";
} else {
var targetSheetName = "Raw Data";
}
var targetSheet = SpreadsheetApp.openById("1bjLKWXUwM6UE4SHz2pbhH3F1E-8GKtvV0870YetOpKo").getSheetByName(targetSheetName);
var targetRange = targetSheet.getRange(targetSheet.getLastRow() + 1, 1,1,activesheet.getLastColumn());
targetRange.setValues(activesheet.getRange(row, 1, 1, activesheet.getLastColumn()).getValues());
activesheet.deleteRow(row);
}