61914

how to pass parameters to a triggered function in google-app-script

I have a array of recipients and a scheduled time at which the mail has to be sent. I am eager in knowing if there is someway to create a trigger to which i can pass these params(recipients)

function send_mail(recipients) { var id = DocumentApp.getActiveDocument().getId(); for(i=0;i<recipients.length;i++) { var url = "https://docs.google.com/feeds/download/documents/export/Export?id="+id+"&exportFormat=html"; var param = { method : "get", headers : {"Authorization": "Bearer " + ScriptApp.getOAuthToken()}, muteHttpExceptions:true, }; var html = UrlFetchApp.fetch(url,param).getContentText(); var raw = Utilities.base64EncodeWebSafe("Subject: Test\r\n" + "To:" +recipients[i]+"\r\n" + "Content-Type: text/html; charset=UTF-8\r\n\r\n" + html+"\r\n\r\n"); var message = Gmail.newMessage(); message.raw = raw; var sentMsg = Gmail.Users.Messages.send(message, recipients[i]); } } function schedule_mail(recipients,schedule) { ScriptApp.newTrigger("send_mail").timeBased().at(schedule).create(); }

If params cannot be passed to a timeBased trigger, then is there any other work around?

Answer1:

There is no direct way to do this. But here is a workaround. Note: This is just the instruction and not the actual code. Check my comments in code.

function send_mail(recipients) { ............ ............ } function triggerHandler(e){ var triggerId = e.triggerUid; //Iterate through all triggers and get the matching triggerId and corresponding recipient send_mail(recipient); } function schedule_mail(recipients,schedule) { var trigger = ScriptApp.newTrigger("triggerHandler").timeBased().at(schedule).create(); var ID = trigger.getUniqueId(); //Save this ID against the recipient details in user properties }

Recommend

  • How to export an InfoPath Form as a PDF from within a WinForm hosted FormControl?
  • Why does this CORS request to a google Drive Sheet fail in Firefox ? (works in Chrome)
  • In Angular 2 how to get @Input value updated inside component?
  • LINQ to Entities does not recognize the method 'System.String ToString()' method, and this
  • Delphi. Analog of Memo/RichEdit
  • Angular5 Service Worker update(SWUpdate) not detecting on firefox. Working on chrome
  • Deploying pre-encrypted configuration files to a production environment
  • Instanciate service on startup in Angular2
  • get passwords from chrome
  • 'doc_del_count' bigger than 'doc_count' on CouchDB
  • c# winform DrawToBitmap offscreen
  • sudo easy_install for SimpleITK not that easy
  • Linking Ghostscript to pypdfocr in Windows Platform
  • in batch how do i use taskkill properly
  • Portable JRE on Linux - possible?
  • Changing Jupyter Notebook start up folder by modifying “start in” not working any more
  • saving file generated by TCPDF
  • How to run “Deployd” on port 80 instead of port 5000 in webserver.
  • Content-Length header not returned from Pylons response
  • Python urlparse: small issue
  • Cross-Platform Protobuf Serialization
  • Can I make an Android app that runs a web view in Chrome 39?
  • Google cloud sdk not working when python points python3
  • Rearranging Cells in UITableView Bug & Saving Changes
  • QuartzCore.framework for Mono Develop
  • RestKit - RKRequestDelegate does not exist
  • Circular dependency while pushing http interceptor
  • How to get icons for entities from eclipse?
  • Linker errors when using intrinsic function via function pointer
  • Revoking OAuth Access Token Results in 404 Not Found
  • Proper way to use connect-multiparty with express.js?
  • Load html files in TinyMce
  • using HTMLImports.whenReady not working in chrome
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass
  • JaxB to read class hierarchy
  • Observable and ngFor in Angular 2
  • Android Heatmap on canvas or ImageView