Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/jsf/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google apps script 如何将GoogleApps脚本合并到1中以最小化GMAIL API调用_Google Apps Script_Google Sheets_Gmail Api - Fatal编程技术网

Google apps script 如何将GoogleApps脚本合并到1中以最小化GMAIL API调用

Google apps script 如何将GoogleApps脚本合并到1中以最小化GMAIL API调用,google-apps-script,google-sheets,gmail-api,Google Apps Script,Google Sheets,Gmail Api,我有9个单独的脚本设置,每5分钟将CSV附件从GMAIL导入Google Sheets。我更愿意经常这样做,但是当我每分钟都尝试执行这些命令时,却收到了临时禁令。如果可能的话,我还想消除这个错误:TypeError:cannotcallmethodgetmessages of undefined。当没有要处理的电子邮件时返回 是否有一种方法可以将每个脚本与一个GMAIL API调用一起执行,而不是单独执行9个?相同的电子邮件地址,每个脚本有不同的标签。 以下是每5分钟执行的9个脚本中的2个: f

我有9个单独的脚本设置,每5分钟将CSV附件从GMAIL导入Google Sheets。我更愿意经常这样做,但是当我每分钟都尝试执行这些命令时,却收到了临时禁令。如果可能的话,我还想消除这个错误:TypeError:cannotcallmethodgetmessages of undefined。当没有要处理的电子邮件时返回

是否有一种方法可以将每个脚本与一个GMAIL API调用一起执行,而不是单独执行9个?相同的电子邮件地址,每个脚本有不同的标签。 以下是每5分钟执行的9个脚本中的2个:

function STSalesImportFromGmail() { 
  //gets first(latest) message with set label
  var threads = GmailApp.getUserLabelByName('Streeterville Sales').getThreads(0,1);     
  var message = threads[0].getMessages()[0];
  var attachment = message.getAttachments()[0];

  // Is the attachment a CSV file
  attachment.setContentTypeFromExtension();
  if (attachment.getContentType() === "text/csv") {                            
    var ss = SpreadsheetApp.getActive();
    var sh = ss.getSheetByName("Streeterville Sales");
    //parses content of csv to array
    var dataString = attachment.getDataAsString();
    var csvData = CSVToArray(dataString);
    // Remember to clear the content of the sheet before importing new data
    sh.clearContents().clearFormats();                                         
    //pastes array to sheet
    var lastRowValue = sh.getLastRow();
    for (var i = 0; i < csvData.length; i++) {
       sh.getRange(i+lastRowValue+1, 1, 1, csvData[i].length).setValues(new Array(csvData[i]));
    } 
    if( message.getSubject().indexOf('END OF DAY SALES DETAILS') !== -1) {
    STlogTodaysSales()
    }
  //marks the Gmail message as read, unstars it and deletes it using Gmail API (Filter sets a star)
  message.markRead();
  message.unstar();
  Gmail.Users.Messages.remove("me", message.getId()); // Added
    }
}
//The code formats the code so it can be entered into the Google Script

function CSVToArray( strData, strDelimiter ){ 
  // Check to see if the delimiter is defined. If not,
  // then default to comma.
  strDelimiter = (strDelimiter || ",");

  // Create a regular expression to parse the CSV values.
  var objPattern = new RegExp(
    (
      // Delimiters.
      "(\\" + strDelimiter + "|\\r?\\n|\\r|^)" +


      // Quoted fields.
      "(?:\"([^\"]*(?:\"\"[^\"]*)*)\"|" +


      // Standard fields.
      "([^\"\\" + strDelimiter + "\\r\\n]*))"
    ),
    "gi"
  );


  // Create an array to hold our data. Give the array
  // a default empty first row.
  var arrData = [[]];

  // Create an array to hold our individual pattern
  // matching groups.
  var arrMatches = null;

  // Keep looping over the regular expression matches
  // until we can no longer find a match.
  while (arrMatches = objPattern.exec( strData )){

    // Get the delimiter that was found.
    var strMatchedDelimiter = arrMatches[ 1 ];

    // Check to see if the given delimiter has a length
    // (is not the start of string) and if it matches
    // field delimiter. If id does not, then we know
    // that this delimiter is a row delimiter.
    if (
      strMatchedDelimiter.length &&
      (strMatchedDelimiter != strDelimiter)
    ){

      // Since we have reached a new row of data,
      // add an empty row to our data array.
      arrData.push( [] );

    }

    // Now that we have our delimiter out of the way,
    // let's check to see which kind of value we
    // captured (quoted or unquoted).
    if (arrMatches[ 2 ]){

      // We found a quoted value. When we capture
      // this value, unescape any double quotes.
      var strMatchedValue = arrMatches[ 2 ].replace(
        new RegExp( "\"\"", "g" ),
        "\""
      );

    } else {

      // We found a non-quoted value.
      var strMatchedValue = arrMatches[ 3 ];

    }

    // Now that we have our value string, let's add
    // it to the data array.
    arrData[ arrData.length - 1 ].push( strMatchedValue );
  }

  // Return the parsed data.
  return( arrData );
  var label = GmailApp.getUserLabelByName("Streeterville Sales");
  label.deleteLabel();
}
function STLaborImportFromGmail() { 
  //gets first(latest) message with set label
  var threads = GmailApp.getUserLabelByName('Streeterville Labor').getThreads(0,1);     
  var message = threads[0].getMessages()[0];
  var attachment = message.getAttachments()[0];

  // Is the attachment a CSV file
  attachment.setContentTypeFromExtension();
  if (attachment.getContentType() === "text/csv") {                            
    var ss = SpreadsheetApp.getActive();
    var sh = ss.getSheetByName("Streeterville Labor");
    //parses content of csv to array
    var dataString = attachment.getDataAsString();
    var csvData = CSVToArray(dataString);
    var range = sh.getRange("A:K");
    // Remember to clear the content of the sheet before importing new data
  range.clear();
    //pastes array to sheet
    var lastRowValue = sh.getLastRow();
    for (var i = 0; i < csvData.length; i++) {
       sh.getRange(i+1, 1, 1, csvData[i].length).setValues(new Array(csvData[i]));
    } 
    if( message.getSubject().indexOf('END OF WEEK LABOR DETAILS') !== -1) {
    STlogWeeksLabor()
    }
  }

  //marks the Gmail message as read, unstars it and deletes it using Gmail API (Filter sets a star)
  message.markRead();
  message.unstar();
  Gmail.Users.Messages.remove("me", message.getId()); // Added

}
function STlogTodaysSales() {
  var todaysSales = SpreadsheetApp.getActive().getRange('Streeterville Sales Log!STSalesImport');
  var logSheet = todaysSales.getSheet();
  logSheet.appendRow(
    todaysSales.getValues()
    .reduce(function(a, b) { return a.concat(b); }) // flatten the 2D array to 1D
  );
}
function STlogWeeksLabor() {
  var ss=SpreadsheetApp.getActive();
  var sh=ss.getSheetByName('Streeterville Labor Log');
  var rg=sh.getRange('STLaborImport');
  var vA=rg.getValues();
  sh.getRange(sh.getLastRow()+1,1,vA.length,vA[0].length).setValues(vA);
}
您说:如果可能的话,我也想消除这个错误:TypeError:cannotcallmethodgetmessages of undefined

这个怎么样:

var threads = GmailApp.getUserLabelByName('Streeterville Sales').getThreads(0,1);
if(threads) {
   var message = threads[0].getMessages()[0];
   ...
至于其余的。我认为这要求有点过分。

您说:如果可能的话,我也想消除这个错误:TypeError:cannotcallmethodgetmessages of undefined

这个怎么样:

var threads = GmailApp.getUserLabelByName('Streeterville Sales').getThreads(0,1);
if(threads) {
   var message = threads[0].getMessages()[0];
   ...
至于其余的。我想这要求有点过分。

你的问题

如果可能的话,我还想消除这个错误:TypeError:cannotcallmethodgetmessages of undefined。当没有要处理的电子邮件时返回

是由于线程为空

要避免此错误,您可以在尝试使用threadscheck属性之前执行以下操作:=空

添加如下检查:如果threads&&threads.length>0,然后继续

关于统一所有Gmail API调用的请求,这是一个非常广泛的问题,不适合堆栈溢出。请阅读并回答这个问题。

您的问题

如果可能的话,我还想消除这个错误:TypeError:cannotcallmethodgetmessages of undefined。当没有要处理的电子邮件时返回

是由于线程为空

要避免此错误,您可以在尝试使用threadscheck属性之前执行以下操作:=空

添加如下检查:如果threads&&threads.length>0,然后继续


关于统一所有Gmail API调用的请求,这是一个非常广泛的问题,不适合堆栈溢出。请阅读和此信息。

请阅读请阅读尝试使用此信息,但仍收到相同的错误。```函数stsaleImportFromgMail{//获取第一条最新消息,设置标签var threads=GmailApp.getUserLabelByName'Streeterville Sales.getThreads0,1;ifthreads{var message=threads[0];var attachment=message.getAttachments[0];}“`当我遇到这样的错误时,我开始仔细观察错误所说的未定义的内容。我认为这是您应该查看的地方。尝试使用此功能,但仍然收到相同的错误。``函数stsaleimportfromgmail{//获取第一条最新消息,设置标签var threads=GmailApp.getUserLabelByName'Streeterville Sales.getThreads0,1;ifthreads{var message=threads[0]。getMessages[0];var attachment=message.getAttachments[0];}“嗯,当我遇到这样的错误时,我开始仔细观察错误所说的未定义的内容。我想这就是你应该看的地方。