1

我正在使用以下腳本在編輯單元格時將行移動到其他表單。我還需要將此腳本應用於Google表單響應表。我在表單回覆表上創建了一個新列,當標記爲「是」時,我需要該行移動到工作簿中的另一個工作表。 Google表單的回覆表單與普通表單不同嗎?該腳本適用於所有其他工作表。腳本編輯器不適用於Google表單響應表

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

回答

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] 

希望幫助!

+0

完美的作品!謝謝! – AZagent

相關問題