我正在Google表格中使用Google脚本功能,根据某些条件将行从一个表格复制到另一个表格。本质上,我想将“请求”移至存档或已确认池中。我想从“确认的池”中将它们完成后移到存档中。
我尝试过几种方式移动代码,包括将代码分成不同的脚本并将其保存在同一脚本中。部分代码可以同时工作-例如,我通常可以将请求移至已确认的池或存档中。最初,我认为问题是我在不同的工作表中使用了同一列。我改变了它,但是没有解决代码。然后,我发现多个“ onEdit”脚本不能在同一工作表中运行,因此我将其中一个脚本的名称更改为onEdit2,然后更改为“ myBookingRequests”。该更改也不起作用。
当我尝试将任何内容从已确认的池移入存档时,代码往往会中断,因此我认为现在的问题是我正在将行移入工作表并从工作表中获取行。有关如何解决此问题或从代码中消除冲突的任何帮助都将有所帮助。谢谢。
两个脚本分开:
function onEdit(event) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if(s.getName() == "Booking Requests" && r.getColumn() == 12 && r.getValue() == "Y") {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Confirmed Bookings");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).copyTo(target);
s.deleteRow(row); }
if(s.getName() == "Booking Requests" && r.getColumn() == 13 && r.getValue() == "Y") {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Archived Bookings");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).copyTo(target);
s.deleteRow(row); }
}
function onEdit2(event) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if(s.getName() == "Confirmed Bookings" && r.getColumn() == 14 && r.getValue() == "Y") {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Archived Bookings");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).copyTo(target);
s.deleteRow(row); }
if(s.getName() == "Confirmed Bookings" && r.getColumn() == 15 && r.getValue() == "Y") {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("Archived Bookings");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).copyTo(target);
s.deleteRow(row); }
}
组合脚本很简单,因为我只需要在同一标题下添加两个额外的if子句。
我还没有收到任何实际的错误消息,因此我假设代码的某些部分与其他部分产生负面影响。防止代码冲突的任何帮助都将非常有用。谢谢。
(而且,我非常没有经验。我仅在需要制作功能性电子表格才能工作时才这样做。)
分离问题区域后的执行日志(预订请求->确认的预订):
[19-07-03 14:20:54:610 CDT] Starting execution
[19-07-03 14:20:54:617 CDT] SpreadsheetApp.getActiveSpreadsheet() [0 seconds]
[19-07-03 14:20:54:618 CDT] SpreadsheetApp.getActive() [0 seconds]
[19-07-03 14:20:54:619 CDT] Spreadsheet.getActiveSheet() [0 seconds]
[19-07-03 14:20:54:620 CDT] SpreadsheetApp.getActive() [0 seconds]
[19-07-03 14:20:54:620 CDT] Spreadsheet.getActiveRange() [0 seconds]
[19-07-03 14:20:54:693 CDT] Sheet.getName() [0.072 seconds]
[19-07-03 14:20:54:694 CDT] Range.getColumn() [0 seconds]
[19-07-03 14:20:54:788 CDT] Range.getValue() [0.093 seconds]
[19-07-03 14:20:54:788 CDT] Range.getRow() [0 seconds]
[19-07-03 14:20:54:875 CDT] Sheet.getLastColumn() [0.086 seconds]
[19-07-03 14:20:54:876 CDT] Spreadsheet.getSheetByName([Confirmed Requests]) [0.001 seconds]
[19-07-03 14:20:54:975 CDT] Sheet.getLastRow() [0.098 seconds]
[19-07-03 14:20:54:976 CDT] Sheet.getRange([995, 1]) [0 seconds]
[19-07-03 14:20:54:977 CDT] Sheet.getRange([4, 1, 1, 12]) [0 seconds]
[19-07-03 14:20:54:977 CDT] Range.copyTo([Range]) [0 seconds]
[19-07-03 14:20:55:084 CDT] Sheet.deleteRow([4]) [0.106 seconds]
[19-07-03 14:20:55:094 CDT] Execution failed: The coordinates of the target range are outside the dimensions of the sheet. (line 12, file "Code") [0.47 seconds total runtime]
答案 0 :(得分:0)
此代码是单个onEdit(e)。
该代码按OP的建议进行工作,并包含许多Logger
语句,这些语句使OP可以在各个阶段对值进行故障排除并确定任何错误的原因。
function onEdit(event) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
var eVal = event.value;
var eSheet = event.source.getActiveSheet().getName();
var ecolumnStart = event.range.columnStart;
var erowStart = event.range.rowStart;
var erowEnd = event.range.rowEnd;
var ecolumnEnd = event.range.columnEnd;
Logger.log("DEBUG: the spreadsheet is "+event.source.getName()+", the sheet = "+eSheet);
Logger.log("DEBUG: Range details - Column Start:"+ecolumnStart+", Column End:"+ecolumnEnd+", Row start:"+erowStart+", and Row End:"+erowEnd);
Logger.log("DEBUG: The Range = "+event.range.getA1Notation()+", and the value = "+eVal);
//Logger.log(JSON.stringify(event))
// STEP#1
// Sheet=Booking Requests
// AND
// column = L (12)
// AND
// value = "Y"
if(s.getName() == "Booking Requests" && r.getColumn() == 12 && r.getValue() == "Y") {
Logger.log("DEBUG: step 1 - if worked");
var row = r.getRow();
var numColumns = s.getLastColumn();
Logger.log("DEBUG: row = "+row+", number of columns = "+numColumns);
var targetSheet = ss.getSheetByName("Confirmed Bookings");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
Logger.log("DEBUG: target range = "+target.getA1Notation());
s.getRange(row, 1, 1, numColumns).copyTo(target);
s.deleteRow(row);
}
else{
Logger.log("DEBUG: step 1 - if failed");
}
// STEP#2
// Sheet=Booking Requests
// AND
// Column = 13 (M)
// AND
// VALUE = "Y"
if(s.getName() == "Booking Requests" && r.getColumn() == 13 && r.getValue() == "Y") {
Logger.log("DEBUG: step 2 - if worked");
var row = r.getRow();
var numColumns = s.getLastColumn();
Logger.log("DEBUG: row = "+row+", number of columns = "+numColumns);
var targetSheet = ss.getSheetByName("Archived Bookings");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
Logger.log("DEBUG: target range = "+target.getA1Notation());
s.getRange(row, 1, 1, numColumns).copyTo(target);
s.deleteRow(row);
}
else
{
Logger.log("DEBUG: step 2 - if failed");
}
// STEP#3
// Sheet=Confirmed Bookings
// AND
// column = N (14)
// AND
// value = "Y"
if(s.getName() == "Confirmed Bookings" && r.getColumn() == 14 && r.getValue() == "Y") {
Logger.log("DEBUG: step 3 - if worked");
var row = r.getRow();
var numColumns = s.getLastColumn();
Logger.log("DEBUG: row = "+row+", number of columns = "+numColumns);
var targetSheet = ss.getSheetByName("Archived Bookings");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).copyTo(target);
s.deleteRow(row);
}
else
{
Logger.log("DEBUG: step 3 - if failed");
}
// STEP#4
// Sheet=Confirmed Bookings
// AND
// column = O (15)
// AND
// value = "Y"
if(s.getName() == "Confirmed Bookings" && r.getColumn() == 15 && r.getValue() == "Y") {
Logger.log("DEBUG: step 4 - if worked");
var row = r.getRow();
var numColumns = s.getLastColumn();
Logger.log("DEBUG: row = "+row+", number of columns = "+numColumns);
var targetSheet = ss.getSheetByName("Archived Bookings");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).copyTo(target);
s.deleteRow(row);
}
else
{
Logger.log("DEBUG: step 4 - if failed");
}
}