脚本编辑器不适用于Google表单响应表

时间:2017-04-05 02:23:56

标签: google-apps-script google-sheets google-spreadsheet-api google-form google-sheets-api

我正在使用下面的脚本在编辑单元格时将行移动到其他工作表。我还需要应用此脚本来处理谷歌表单响应表。我在表单响应表上创建了一个新列,当标记为“是”时,我需要将该行移动到工作簿中的另一个工作表。谷歌表单响应表是否与普通表格不同?该脚本适用于所有其他工作表。

function onEdit() {
var sheetNameToWatch = "New Listings";
var columnNumberToWatch = 52;
var valueToWatch = "Yes";
var sheetNameToMoveTheRowTo = "New Listings Completed";

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());
}
}

1 个答案:

答案 0 :(得分:0)

如果您将代码修改为copyTo而不是moveTo,则会给出预期的行为。

sheet.getRange(range.getRow(), 1, 1,sheet.getLastColumn()).copyTo(targetRange);

修改后的代码:

function onEdit() {
var sheetNameToWatch = "New Listings";
var columnNumberToWatch = 52;
var valueToWatch = "Yes";
var sheetNameToMoveTheRowTo = "New Listings Completed";

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()).copyTo(targetRange);
sheet.deleteRow(range.getRow());
}
}

正如您所猜测的那样,原因很可能是响应表是一张独特的表格。另外,如果你查看你的执行记录,就会出现如下错误:

[17-04-04 21:51:56:645 CDT] Execution failed: Cannot cut from form data. Use copy instead. (line 123, file "SOhelp2") [0.405 seconds total runtime]

希望有所帮助!