我有我的谷歌問題的Apps腳本在我的電子表格 - 此代碼的工作非常好,但是我有一個問題,即有時它會刪除該行再經過直下一行?我花了幾個小時來質疑這個問題,它在我自己完美地執行代碼時工作正常,但是當我從電子表格執行代碼時,它不起作用?Google Apps腳本 - sheet.deleterow()刪除行和下一行?
該計劃是當用戶將Y值輸入到單元格中時,這意味着某人已完成並將從學習表單中的信息複製到完成的工作表中 - 當此代碼觸發編輯時,有時會刪除該行它應該,但有時然後下面的行 - 爲什麼是這樣,並有無論如何修復它?
function myOnEdit(e) {
// -----------------------------------------------------------------------------
// This function will run whenever a cell is edited
// -----------------------------------------------------------------------------
// -------------------------------------------------------------------------
// Get Active sheet
// -------------------------------------------------------------------------
var sheet = ss.getActiveSheet();
// -------------------------------------------------------------------------
// -------------------------------------------------------------------------
// Get Active cell
// -------------------------------------------------------------------------
var mycell = ss.getActiveSelection();
var cellcol = mycell.getColumn();
var cellrow = mycell.getRow();
// -------------------------------------------------------------------------
if (sheet.getName() == "In Learning"){ // Check to see if the sheet is "In Learning"
if (cellcol == 19 && cellrow > 2){ // Check to see if the column is the learner complete column
if (mycell.getValue() == "Y"){
var targetSheet = ss.getSheetByName("Completions"); // Get the Completions Sheet
var targetrow = getFirstEmptyRow(targetSheet)+1; // Find the last row (prevents loads of empty rows horray)
var target = targetSheet.getRange(targetrow, 1); // Copy the learner information
sheet.getRange(cellrow, 1, 1, cellcol-1).moveTo(target); // Move the information
sheet.deleteRow(cellrow); // Delete it from in learning
SpreadsheetApp.getActiveSpreadsheet().toast('Learner has been completed', 'Completed', 3); // Alert the user
sendEmail(learnerObjects, (cellrow-1));
deleted = true;
}
else if (mycell.getValue() == "W"){
var targetSheet = ss.getSheetByName("Withdrawals"); // Get the withdrawals sheet
var targetrow = getFirstEmptyRow(targetSheet)+1; // Find the last row
sheet.getRange(cellrow, 1, 1, 4).moveTo(targetSheet.getRange(targetrow, 1)); // Copy learner information and move it
sheet.getRange(cellrow, 10, 1, 1).moveTo(targetSheet.getRange(targetrow, 5)); // Copy learner information and move it
sheet.getRange(cellrow, 6, 1, 2).moveTo(targetSheet.getRange(targetrow, 6)); // Copy learner information and move it
sheet.deleteRow(cellrow); //Delete it from in learning
SpreadsheetApp.getActiveSpreadsheet().toast('Learner has been Withdrawn', 'Withdrawn', 3); // Alert the user
deleted = true;
}
else {
mycell.setValue("N");
}
}
switch(cellcol){ // Otherwise check to see if the user is messing with formulas
case 12: mycell.setFormula("=CONTINUE(L2, " + cellrow-1 + ", 1)");
SpreadsheetApp.getActiveSpreadsheet().toast('Formula Corrected', 'Warning', 3);
break;
case 14: mycell.setFormula("=CONTINUE(N2, " + cellrow-1 + ", 1)");
SpreadsheetApp.getActiveSpreadsheet().toast('Formula Corrected', 'Warning', 3);
break;
case 15: mycell.setFormula("=CONTINUE(O2, " + cellrow-1 + ", 1)");
SpreadsheetApp.getActiveSpreadsheet().toast('Formula Corrected', 'Warning', 3);
break;
}
if (deleted == false && sheet.getRange(cellrow,19).getValue() == "")
{
sheet.getRange(cellrow,19).setValue("N");
}
}
if (sheet.getName() == "Completions"){
if (cellcol == 19 && cellrow > 1){ // Check to see if the column is the learner complete column
if (mycell.getValue() == "Y"){
var targetSheet = ss.getSheetByName("In Learning"); // Get the Completions Sheet
var targetrow = getFirstEmptyRow(targetSheet)+1; // Find the last row (prevents loads of empty rows horray)
var target = targetSheet.getRange(targetrow, 1); // Copy the learner information
sheet.getRange(cellrow, 1, 1, cellcol-1).moveTo(target); // Move the information
sheet.deleteRow(cellrow); // Delete it from in learning
SpreadsheetApp.getActiveSpreadsheet().toast('Learner completion has been revoked', 'Revoked Completion', 3); // Alert the user
deleted = true;
}
else {
mycell.setValue("N");
}
}
}
}
也許確實正在執行代碼時兩次是自動的。放一個'Browser.msgBox'來確保。 –
這似乎是這樣,我剛剛在那裏測試它,並彈出一個msgbox兩次。有沒有其他方法? – Andy
我無法重現這一點,請嘗試啓動一個新的'onEdit'來測試它是否運行兩次。然後在小步驟中使其更加複雜,直到達到當前的代碼。一直測試,直到你指出問題。如果您可以附帶一個非常簡單的代碼,任何人都可以複製和重現問題,那麼幫助起來會更容易。 –