我不明白為什么我的函式無法識別 onEdit 函式中的“e”?你有想法嗎?非常感謝,如果您能 在我嘗試除錯時幫助我解決錯誤
function onEdit(e){
var feuille = e.source;
if(SpreadsheetApp.getActiveRange().getColumn()==7){ //the 7th column is the " /-" column
var i = e.source.getActiveRange().getRow(); //i is the line of the change
var k = 408 i; // k is the line where we put the date
var l = 2; //l is the column where we put the date and the quantity
var cellDate = feuille.getRange(k,l);
while (isEmpty(cellDate)==false){ //the column of the date is incremented
l=l 1;
}
feuille.getRange(k,l).setValue(new Date());
const stockDate = feuille.getRange(i,8).getValue();
feuille.getRange(k 1,8).setValue(stockDate);
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getRange("G2:G403").setValue('0'); ////we reset the " /-" column to zero
}
}
uj5u.com熱心網友回復:
該onEdit(e)功能是一個簡單的觸發器,旨在在您手動編輯電子表格時自動運行。在這種情況下,事件物件e被正確填充。
如果您onEdit(e)在腳本編輯器中盲目運行函式,e則不會填充 event 引數,從而導致您提到的錯誤。
您可以onEdit(e)通過撰寫一個包裝函式來除錯您的函式,該函式創建一個物件并在呼叫時將其用作引數onEdit(e),如下所示:
function testOnEdit() {
const e = {
source: SpreadsheetApp.getActive(),
};
onEdit(e);
}
然后除錯testOnEdit()函式。
請參閱事件物件。
uj5u.com熱心網友回復:
根據事件物件的檔案:
簡單的觸發器和可安裝的觸發器讓 Apps Script 在發生特定事件時自動運行函式。當觸發器觸發時,Apps 腳本將事件物件作為引數傳遞給函式,通常稱為 e。事件物件包含有關導致觸發器觸發的背景關系的資訊。
因此,e是函式上傳遞的事件物件onEdit。
您收到此訊息的原因是,僅在進行編輯(即觸發onEdit觸發器)時才傳遞物件。
如果您從編輯器運行該函式,那么您將收到此錯誤,因為沒有進行任何編輯,因此沒有傳遞任何編輯物件,因此e為空。
參考
- Apps 腳本事件物件。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/465835.html
上一篇:Zip已創建,但其中沒有檔案
