Getting a Google Form Script generated email to pipe data into Reponse Sheet

后端 未结 2 507
梦谈多话
梦谈多话 2021-01-27 10:49

I am currently trying to setup an approval workflow. I\'m fairly junior when it comes to some of this stuff. But so far have it working at respectable level to fit our needs wit

2条回答
  •  别那么骄傲
    2021-01-27 11:23

    The approval workflow that James Ferreira originally wrote was intentionally very basic. Enhancing it to provide correlation between approvals and the original requests is simple in concept, yet complicates the code because of details and limitations.

    Things that need to considered:

    • Form responses include a timestamp, which we can use as a sort of "serial number" for responses, since it has a very high probability of being unique in an application like this.

      • We can pass this identifier along with the rest of the URL embedded in the approval email, which will give it back as a parameter to the webapp, in turn.

      • Passing an object like a timestamp between applications can be troublesome. To ensure that the URL embedded in the approval email works, we need to encode the string representation of the timestamp using encodeURIComponent(), then decode it when the webapp consumes it.

      • Searching for matches is simplified by use of ArrayLib.indexOf() from Romain Vaillard's 2D Array library. This function converts all array data to strings for comparisons, so it fits nicely with our timestamp.

      • Alternatively, we could put effort into creating some other identifier that would be "guaranteed" to be unique.

    • The approval-response web app (doGet()) does not understand what "activeSpreadsheet" means, even though it is a script contained within a spreadsheet. This means that we need a way to open the spreadsheet and record the approval result. To keep this example simple, I've assumed that we'll use a constant, and update the code for every deployment. (Ick!).

    screenshot

    So here's the Better Expense Approval Workflow script.

    // Utilizes 2D Array Library, https://sites.google.com/site/scriptsexamples/custom-methods/2d-arrays-library
    //                            MOHgh9lncF2UxY-NXF58v3eVJ5jnXUK_T
    
    function sendEmail(e) {
      // Response columns: Timestamp    Requester Email Item    Cost
      var email = e.namedValues["Requester Email"];
      var item = e.namedValues["Item"];
      var cost = e.namedValues["Cost"];
      var timestamp = e.namedValues["Timestamp"];
    
      var url = ScriptApp.getService().getUrl();
    
      // Enhancement: include timestamp to coordinate response  
      var options = '?approval=%APPROVE%×tamp=%TIMESTAMP%&reply=%EMAIL%'
             .replace("%TIMESTAMP%",encodeURIComponent(e.namedValues["Timestamp"]))
             .replace("%EMAIL%",e.namedValues["Requester Email"])         
      var approve = url+options.replace("%APPROVE%","Approved"); 
      var reject = url+options.replace("%APPROVE%","Rejected");
    
      var html = ""+
                    "

    Please review


    "+ "Request from: " + email + "
    "+ "For: "+item +", at a cost of: $" + cost + "

    "+ "Approve
    "+ "Reject
    "+ ""; MailApp.sendEmail(Session.getEffectiveUser().getEmail(), "Approval Request", "Requires html", {htmlBody: html}); } function doGet(e){ var answer = (e.parameter.approval === 'Approved') ? 'Buy it!' : 'Not this time, Keep saving'; var timestamp = e.parameter.timestamp; MailApp.sendEmail(e.parameter.reply, "Purchase Request", "Your manager said: "+ answer); // Enhancement: store approval with request var sheet = SpreadsheetApp.openById(###Sheet-Id###).getSheetByName("Form Responses"); var data = sheet.getDataRange().getValues(); var headers = data[0]; var approvalCol = headers.indexOf("Approval") + 1; if (0 === approvalCol) throw new Error ("Must add Approval column."); // Record approval or rejection in spreadsheet var row = ArrayLib.indexOf(data, 0, timestamp); if (row < 0) throw new Error ("Request not available."); // Throw error if request was not found sheet.getRange(row+1, approvalCol).setValue(e.parameter.approval); // Display response to approver var app = UiApp.createApplication(); app.add(app.createHTML('

    An email was sent to '+ e.parameter.reply + ' saying: '+ answer + '

    ')) return app }

提交回复
热议问题