简体   繁体   English

Google表格。 确认电子邮件脚本。 使用编辑网址

[英]Google Form. Confirmation Email Script. With edit url

I have two working trigger functions in Google Script that fire when a form response spreadsheet gets a new submission. 我在Google脚本中有两个工作触发功能,当表单响应电子表格获取新的提交时会触发。 One inserts the "edit your submission" url into the spreadsheet. 一个将“编辑您的提交” URL插入电子表格。 The other looks up the response's email and sends them a confirmation. 另一个查询响应的电子邮件,并向他们发送确认。

What I'm having a hard time understanding is how to populate the url first , and then send an email containing that url. 就是我有一个很难理解的是如何填充URL, 然后发送包含该URL的电子邮件。

( Google Script is different to debug than js in the browser :\\ ) Google脚本与浏览器中的js调试不同:\\

Setup triggers 设置触发器

function Initialize() {

        var triggers = ScriptApp.getScriptTriggers();

        for (var i in triggers) {
            ScriptApp.deleteTrigger(triggers[i]);
        }

        ScriptApp.newTrigger("SendConfirmationMail")
            .forSpreadsheet(SpreadsheetApp.getActiveSpreadsheet())
            .onFormSubmit()
            .create();

           assignEditUrls();
    }

On form submit, searches for column titled "Clients Email", and sends a formatted email to them. 在提交表单时,搜索标题为“客户电子邮件”的列,然后向他们发送格式化的电子邮件。

    function SendConfirmationMail(e) {

        try {

            var ss, cc, sendername, subject, columns;
            var message, value, textbody, sender;
            var url;

            // This is your email address and you will be in the CC
            cc = Session.getActiveUser().getEmail();

            // This will show up as the sender's name
            sendername = "XXXX";

            // Optional but change the following variable
            // to have a custom subject for Google Docs emails
            subject = "Form Complete: Mobile App - Client Questionnaire";



            // This is the body of the auto-reply
            message = "Confirmation text here.<br><br>Thanks!<br><br>";

            ss = SpreadsheetApp.getActiveSheet();
            columns = ss.getRange(1, 1, 1, ss.getLastColumn()).getValues()[0];

            // This is the submitter's email address
            sender = e.namedValues["Clients Email"].toString();

            // Only include form values that are not blank
            for ( var keys in columns ) {
                var key = columns[keys];
                if ( e.namedValues[key] ) {
                    message += key + ' :: '+ e.namedValues[key] + "<br />"; 
                }
            }

            textbody = message.replace("<br>", "\n\n");

            GmailApp.sendEmail(sender, subject, textbody, 
                                {cc: cc, name: sendername, htmlBody: message});

        } catch (e) {
            Logger.log(e.toString());
        }

    }

Separate function. 分开的功能。 Looks up form and applies the edit URL to the 26th column. 查找表单并将编辑URL应用于第26列。

    function assignEditUrls() {
      var form = FormApp.openById('10BVYipGhDa_AthabHE-xxxxxx-hg');
        //enter form ID here

      var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Form Responses');

        //Change the sheet name as appropriate
      var data = sheet.getDataRange().getValues();
      var urlCol = 26; // column number where URL's should be populated; A = 1, B = 2 etc
      var responses = form.getResponses();
      var timestamps = [], urls = [], resultUrls = [];

      for (var i = 0; i < responses.length; i++) {
        timestamps.push(responses[i].getTimestamp().setMilliseconds(0));
        urls.push(responses[i].getEditResponseUrl());
      }
      for (var j = 1; j < data.length; j++) {

        resultUrls.push([data[j][0]?urls[timestamps.indexOf(data[j][0].setMilliseconds(0))]:'']);
      }
      sheet.getRange(2, urlCol, resultUrls.length).setValues(resultUrls);  
    }

Programmers are provided no direct control over the order that trigger functions will be invoked, when they are dependent on the same event. 当程序员依赖于同一事件时,不会直接控制触发函数的调用顺序。

In your situation though, there are a couple of options available. 不过,根据您的情况,有几个可用的选项。

  1. Use only one trigger function for the event, and have it invoke the current functions in the order you require. 仅对事件使用一个触发函数,并使其按所需顺序调用当前函数。 (Alternatively, set assignEditUrls() up as the only trigger function, and have it call SendConfirmationMail() after completing its task. (或者,将assignEditUrls()设置为唯一的触发函数,并在完成任务后调用SendConfirmationMail()

  2. Have SendConfirmationMail() check for the availability of the Edit URL, and call Utilities.sleep() if the URL isn't ready yet. SendConfirmationMail()检查Edit URL的可用性,如果URL还没有准备好,请调用Utilities.sleep() (In a loop, until ready.) (循环执行,直到准备好。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过Python脚本以HTML格式编写包含jQuery和CSS的电子邮件。 - Write an email in HTML form containing jQuery and CSS through a Python script. PHP电子邮件脚本。 可能不发送电子邮件? - PHP email script. Possibly not sending emails? Google Form邮件通知脚本。 电子邮件始终以“我”作为发件人 - Google Form mail notification script. Emails always have “me” as sender simpleCart.js 使用 PHP 脚本将表单输入和购物车项目发送到 EMAIL。 - 无法发送表单输入 - simpleCart.js send form input and cart item to EMAIL using PHP script. - cannot sent form input Magento编辑电子邮件订单确认模板不在后端 - Magento Edit Email Order Confirmation Template NOT in the backend 电子邮件管道不适用于PHP脚本。 本地传递失败错误 - Email piping not working for PHP script. Local delivery failed error 从批处理脚本发送电子邮件。 布拉特不起作用 - Send email from a batch script. Blat doesn't work Google表单完成后发送确认电子邮件,以便用户可以看到更新响应 - Send confirmation email after google form completion so user can see update response 流星-密码恢复/电子邮件确认动态网址 - Meteor - Password recovery / Email confirmation dynamic url 验证使用Google Apps脚本在Google表单中输入的电子邮件 - Verify Email Entered in a Google Form Using Google Apps Script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM