Google onformsubmit触发发送最后一行提交以同时提交

时间:2019-01-10 12:49:30

标签: google-apps-script google-sheets google-form eventtrigger

对于Google App脚本来说还是很新,但是设法设置了一个表单,该表单会填充电子表格,然后发送带有pdf附件的电子邮件,并根据用户数据生成报告。我的问题是同时提交。表格附件通过电子邮件发送给最后一个答复者,其次数等于同时提交的次数。幸运的是,根据最近的受访者信息

我已经尝试了SpreadsheetApp.flush()和LockService.getScriptLock(),但未解决问题(可能是由于使用不正确)。我在此附上我拥有的代码。帮助将不胜感激。谢谢

此处提供了更新的代码

//automatically send email with cash flow attached when form is submitted

function onFormSubmit(e) {
// change onFormSubmit() to onFormSubmit(e) when based on event otherwise just onFormSubmit()    

  var sourceSpreadSheet = SpreadsheetApp.openById("spreadsheetID");
  var responseSheet = ss.getSheetByName("Form responses 1") ;

  var data = e.range.getValues();

  //var replied = data.offset(0, e.range.getNumColumns(),0,0);

  //Logger.log(data)

  //change to data = e.range when based on event alternatively responseSheet.getRange("A5:AA26").getValues();

  //Get a lock on this script, because we're about to modify a shared resource
  var lock = LockService.getScriptLock()

  //Wait 30 seconds for other processes to finish
  lock.waitLock(30000);

  //Logger.log(data[0][2]); //- email address

  var email = data[0][2];
  var name = data[0][1];
  var subject = "DelQS - Estimated cash flow for your project";

  //Generate attachment

  hideSheetsforPrint();

  //additional parameters for exporting the sheet as a pdf
   var url_ext = 'export?exportFormat=pdf&format=pdf'   //export as pdf

      // Print either the entire Spreadsheet or the specified sheet if optSheetId is provided
      +  ('&id=' + sourceSpreadSheet())

      // following parameters are optional...
      + '&size=A4'          // paper size
      + '&portrait=false'    // orientation, false for landscape
      + '&fitw=true'        // fit to width, false for actual size
      + '&sheetnames=false&printtitle=false&pagenumbers=false'  //hide optional headers and footers
      + '&gridlines=false'  // hide gridlines
      + '&printnotes=false' // remove notes
      + '&fzr=false';       // do not repeat row headers (frozen rows) on each page

   var options = {
    headers: {
      'Authorization': 'Bearer ' +  ScriptApp.getOAuthToken()
    }
  }

   var response = UrlFetchApp.fetch("https://docs.google.com/spreadsheets/" + url_ext, options);
   var blob = response.getBlob().setName('Estimated cash flow.pdf');

  showAllSheets();

  var projectName = data[0][3]; //project name (optional)
  var htmlBody = HtmlService.createTemplateFromFile("notificationCashFlowEmail");

  //set the values for placeholders in the html file
  htmlBody.name = name;

  //evaluate and get the html file
  var emailHtml = htmlBody.evaluate().getContent();

  GmailApp.sendEmail(email, subject, emailHtml, {htmlBody: emailHtml,attachments: [blob]});

  var d = new Date();

  //responseSheet.getRange(replied).setValue(d); //optional to below line
  responseSheet.getRange(responseSheet.getLastRow(),responseSheet.getLastColumn()).setValue(d);

   SpreadsheetApp.flush();

  //Release the lock so that other processes can continue
  lock.releaseLock();

}

0 个答案:

没有答案