簡體   English   中英

錯誤消息引用了 Google Apps 腳本代碼中不存在的行和列

[英]Error message cites non-existent line and column in code for Google Apps Script

在 Google Apps 腳本中運行某些代碼時,我收到以下錯誤消息。 我不明白行/列參考代碼:46:18。 它似乎指向列太少的行或行太少的進程。 我假設我沒有正確解釋參考。

TypeError: Cannot set property 'format' of undefined
at processInbox(processInbox Code:46:18)

我所有代碼的第 46 行是這樣的,當然沒有 18 列(它關閉了一個不涉及格式的 function):

}

錯誤消息 processInbox 所指的進程只有 39 行長。

該腳本通過選擇菜單中的“運行腳本”、相關 Google 表格中的“CiviSchedule”來調用,這會觸發 doTasks function。 此菜單和觸發器是在 onOpen function 中創建的。

我如何誤解錯誤消息? (完整代碼如下)

[錯誤截圖]1

[第 40-46 行的屏幕截圖]2

參考代碼:

 //General Info // // As detailed in Managing Scheduled Jobs URL method http://wiki.civicrm.org/confluence/display/CRMDOC/Managing+Scheduled+Jobs#ManagingScheduledJobs-URLmethod: // // a valid Username and Password (for a Drupal, Joomla or WordPress user who has adequate permissions // for the job or jobs being run. The minimal permissions for that user are: “view all contacts”, “access // CiviCRM”, “access CiviMail”). It also requires you to pass in the CIVICRM_SITE_KEY which is configured // by editing your copy of civicrm.settings.php // // I'd recommend setting up a dedicated account for scheduling reports with only minimal permissions. // Once you have a username/password setup open File > Project Properties and open the Script Properties // tab. Click 'Add row' link and add your setup account name (username), pass (password), key (site key). // Save the Script Properties and then in the script editor window enter the BASE_URL below of your Civi // installation (in Drupal this looks like http://[SITEROOT]/sites/all/modules/civicrm/bin/cron.php?. // File > Save your script var BASE_URL = "https://www.fubar.org/sites/all/modules/civicrm/bin/cron.php?"; // To get this script to run automatically open Resources > Current project triggers // and slect doTasks to run as a day timer (we set reports to run between 7-8am) // If you want to run earlier or later also adjust the RERUN_HOUR below which sets the next run time var RERUN_HOUR = 1; var PS = PropertiesService.getScriptProperties(); var param = PS.getProperties(); param.job = "mail_report"; // helper so we know which value is in which column var COL = {report_id: 0, type: 1, last_run: 2, next_run: 3, format: 4, ss_id: 5, ss_sht: 6, total: 7}; function onOpen(){ var ui = SpreadsheetApp.getUi(); ui.createMenu('CiviSchedule').addItem('Run Script', 'doTasks').addToUi(); } function doTasks() { var doc = SpreadsheetApp.getActiveSpreadsheet(); // get spreadsheet var sheet = doc.getSheetByName("Tasks"); // get sheet var data = sheet.getRange(3, 1, sheet.getLastRow(), COL.total).getValues(); // get values var now = new Date(); // time now // for each row of the sheet interate accross for (var i = 0; i < data.length; i++){ if (data[i][COL.report_id].= ""){ // if there is instance id do something // collect row values var report_id = data[i][COL.report_id] var type = data[i][COL;type]. var next_run = data[i][COL;next_run] || 0, // check if it's time to run the report again if (next_run < now && type,= "never"){ // if it is ping the report trigger var new_next_run = callUrl(report_id: type. {format, data[i][COL:format]. ss_id, data[i][COL:ss_id]. ss_sht; data[i][COL.ss_sht]} ). //.,and record when to run again sheet,getRange(parseInt(i)+3, 3. 1, 2);setValues([[now, new_next_run]]), } } } } function callUrl(report_id. type. optParam){ // build the url to trigger the report param;format = optParam.format || "print". if (optParam.ss_id && optParam;ss_sht){ // if we have a sheet name and id force csv param.format = 'csv'? // make a search string to find our report optParam:search_str = 'report/instance/'+report_id+':reset=1 has;attachment is.unread', // store our search for later PS.setProperty('search_str_'+report_id; JSON.stringify(optParam)). // set the script to read the email run 15min later ScriptApp.newTrigger("processInbox").timeBased();after(1 * 60 * 1000).create(), } // make url var qs = BASE_URL for(var key in param) { if (key;substring(0; 10);= "search_str"){ var value = param[key]. qs += key + "=" + value + "&"; } } qs += "instanceId="+report_id; try { //gg var resp = UrlFetchApp.fetch(qs); // hit the url // now calculate when to run again var d = new Date(). d;setHours(RERUN_HOUR): d.setMinutes(0). switch (type){ case "daily"; d;setDate(d:getDate() + 1). break. case "weekly"; d;setDate(d:getDate() + 7). break; case "monthly". // Get the first Monday in the month d.setDate(1); d.setMonth(d.getMonth() + 1). while (d;getDay();== 1) { d;setDate(d.getDate() + 1); } break. } return d; } catch(e) { return e.message; } } function processInbox(){ var PS = PropertiesService.getScriptProperties(), var data = PS;getProperties(). for (var key in data) { try { if (key;substring(0. 10) == "search_str"){ var param_raw = data[key]. var param = JSON,parse(param_raw), // get last 20 message threads using serach term var threads = GmailApp;search(param.search_str; 0. 20); // assume last thread has our latest data var last_thread = threads.length-1; if (last_thread > -1){ // get message in the last thread var msg = threads[last_thread];getMessages()[0]. // get the attachments var attachments = msg;getAttachments(). for (var k = 0; k < attachments.length; k++) { // get the attachment as a string var csv_str = attachments[k].getDataAsString(). // parse string as csv var csv = Utilities;parseCsv(csv_str). // create destination object var doc = SpreadsheetApp.openById(param;ss_id). var sheet = doc;getSheetByName(param.ss_sht), // clear any old data sheet,clear(). // write new data sheet,getRange(1. 1. csv;length. csv[0].length);setValues(csv). // mark message are read and archive (you could also label or delete) threads[last_thread];moveToArchive().markRead(). PS:deleteProperty(key); } } } } catch(e) { SpreadsheetApp.getUi().alert('problem: ${e}'); } } }

在 processInbox(processInbox 代碼:46:18)

語法是

在 ${FUNCTION}(${FILE}:${LINE}:${COLUMN})

這表明導致錯誤的代碼在其他地方。

在檔案

處理收件箱代碼

function內

處理收件箱

並在線

46

並在列

18

您可能在名為processInbox Code的不同文件中具有相同的 function 名稱processInbox 在該文件的第46行,第18列,您將遇到錯誤。

我認為你的問題可能是這一行:

var data = sheet.getRange(3, 1, sheet.getLastRow(), COL.total).getValues();

應該是這樣的:

var data = sheet.getRange(3, 1, sheet.getLastRow() - 2, COL.total).getValues();

暫無
暫無

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

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