簡體   English   中英

類似的應用程序腳本不一樣 - 為什么?

[英]Similar Apps Scripts Don't Work the Same - Why?

由觸發器編輯器中設置的觸發器觸發的同一項目中的兩個幾乎相同的功能。

onWrongEdit(e)(由 onEdit 觸發)有效,但是當我添加“var sheet = e.range.getSheet().getName();”時到 onWrongInsert(e)(由 onChange 觸發)第二個 function 失敗,即使它在添加該 var 行之前工作。

我到底錯過了什么?

感覺很傻。 善待:)

function onWrongEdit(e){
  var sheet = e.range.getSheet().getName();
  var rangeCol = e.range.getColumn();
    if((sheet == 'Campaign Pipeline' && (rangeCol === 2 || rangeCol === 8 || rangeCol === 2)) || (sheet == 'Onboarding Pipeline' && (rangeCol === 2 || rangeCol === 3 || rangeCol === 4 || rangeCol === 5 || rangeCol === 11 || rangeCol === 12 ))){
      SpreadsheetApp.getUi().alert("STOP. Don't edit grey shaded cells. To fix, press CTRL+Z to undo. This error message will appear again, just ignore it.");
  }
}
function onWrongInsert(e){
  var sheet = e.range.getSheet().getName(); // The line that messes it up.
    if (e.changeType == 'INSERT_ROW'){  
    SpreadsheetApp.getUi().alert("STOP. You created a row the wrong way. Delete the row and use the 1POT menu.");
    }
}

您的答案來自執行頁面

Aug 25, 2022, 11:19:26 AM   Info    {"authMode":"FULL","changeType":"INSERT_ROW","source":{},"triggerUid":"","user":{"email":"","nickname":""}}


Aug 25, 2022, 11:19:26 AM   Error   TypeError: Cannot read property 'getSheet' of undefined
    at onWrongInsert(ag1:19:23)

這是因為在事件 object for onChange 中沒有范圍元素

我重寫了它們,如下所示:

function onWrongEdit(e){
  Logger.log(JSON.stringify(e));
  var name = e.range.getSheet().getName();
    if((name == 'Sheet0' && (e.range.columnStart === 2 || e.range.columnStart === 8)) || (name == 'Sheet1' && (e.range.columnStart === 2 || e.range.columnStart === 3 || e.range.columnStart === 4 || e.range.columnStart === 5 || e.range.columnStart === 11 || e.range.columnStart === 12 ))){
      SpreadsheetApp.getUi().alert("STOP. Don't edit grey shaded cells. To fix, press CTRL+Z to undo. This error message will appear again, just ignore it.");
  }
}
function onWrongInsert(e){
  Logger.log(JSON.stringify(e));
  var sheet = e.range.getSheet().getName(); // The line that messes it up.
    if (e.changeType == 'INSERT_ROW'){  
    SpreadsheetApp.getUi().alert("STOP. You created a row the wrong way. Delete the row and use the 1POT menu.");
    }
}

function createTriggers() {
  ScriptApp.newTrigger('onWrongEdit').forSpreadsheet(SpreadsheetApp.getActive()).onEdit().create();
  ScriptApp.newTrigger('onWrongInsert').forSpreadsheet(SpreadsheetApp.getActive()).onChange().create();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM