【问题标题】:Google Form > Google Sheet > Google Code = email notificationGoogle Form > Google Sheet > Google Code = 电子邮件通知
【发布时间】:2016-06-28 20:54:52
【问题描述】:

我不确定我应该在哪里发布这个。我希望这是正确的地方。

我有一个 Google 表单,填写表单后,回复会自动输入到 Google 表格中。由于我是所有者,因此我设置了默认的 Google 通知,它会在填写 Google 表单时告诉我表单已更改:

流程是:填写 Google 表单 > 使用表单的回复更新 Google 表格 > 向我的电子邮件地址发送电子邮件通知

我已将 Google 表格分享给我公司的其他用户。他们拥有工作表的编辑权限。我希望该用户在表单发生更改时也收到通知。

因此过程将是:填写 Google 表单 > 使用表单的回复更新 Google 表格 > 向我的电子邮件通知和其他用户的电子邮件地址发送电子邮件

我已经在网上搜索过,因为它在 Google Apps 中还不可能(它只会通知所有者)。建议在我的 Google 表格上创建自定义 Google 代码,如下所示:

function myFunction() {
    MailApp.sendEmail("myemailaddress@mybusiness.com","Notification - Change in Spreadsheet","Notification - There has been a change in the Spreadsheet. Please view the change here: (I've put the url of sheet here) ");

    MailApp.sendEmail("theotheruser@mybusiness.com","Notification - Change in Spreadsheet","Notification - There has been a change in the Spreadsheet. Please view the change here: (I've put the url of sheet here) ");
}

当我从代码编辑器运行此代码时,它会向我和其他用户发送我想要的自定义 Google 代码通知电子邮件。

当我填写表格时,表格中的回复会更新,我会收到来自官方 Google 通知的更改电子邮件通知。其他用户和我没有收到自定义 Google 代码电子邮件通知。

我在触发器部分查看了 Google 代码网站,但不知道如何编写这部分。 我想我需要在代码上写一个触发器,当工作表发生变化时运行自定义代码。

因此填写了 Google 表单 > 使用表单的响应更新了 Google 表格 > Google 表格中的更改触发了触发器 > 触发器运行自定义 Google 代码 > 其他用户和我收到了自定义 Google 代码电子邮件通知。

任何人都可以帮助解决触发代码部分,甚至可以推荐不同的解决方案吗?

谢谢。

【问题讨论】:

    标签: javascript google-apps-script google-code


    【解决方案1】:

    您可以在脚本编辑器的“资源”->“当前项目的触发器”菜单项中添加触发器。

    然后按以下格式创建触发器:

    “myFunction”->“来自电子表格”->“更改时”

    【讨论】:

    • 您可以为文档的所有者创建触发器,但不能为其他任何人创建触发器,即使您已将其共享给其他人。这很棘手,因为它仍会将更改通知电子邮件发送给所有者,而不是其他编辑。
    【解决方案2】:

    对于触发器,我之前做过类似的代码,但那是针对事件 onChange。您在这里显然需要的是“OnFormsubmit”触发器。下面是我使用带有内联 cmets 的自定义菜单制作的代码;

    // Written by Madzihi
    // on a nice Sunday afternoon
    
    function onOpen(){    //Setting up all the custom menu components
            var ss = SpreadsheetApp.getActiveSpreadsheet();    //Get the current active Spreadsheet
            ss.addMenu('Custom Setup',[{name:'Select Sheet',   //Assemble all the menu's component
                                    functionName:'openSheet'},{name:'Set Trigger',functionName:'triggerSet'},
                                    {name:'Delete Trigger',functionName:'delTrigger'}]);
            ss.addEditor(Session.getActiveUser().getEmail());   //This line set up so editor(s) don't have to run manually the script to gain authorization
    }
    function triggerSet(){    //Function to setting up the triggers for current project 
        var ss = SpreadsheetApp.getActiveSpreadsheet();
        var ui = SpreadsheetApp.getUi();    //Get the UI for setting trigger
        var triggers = ScriptApp.getProjectTriggers();    //Get the current project's trigger
        var respond = ui.alert('Setting up new trigger', ui.ButtonSet.OK_CANCEL);    //Set the UI prompt message and 'OK/CANCEL' buttons
            if(respond ==ui.Button.OK && triggers <=1){   //Set the trigger if button 'OK' is clicked and prevent trgger to be installed more than 1
            ScriptApp.newTrigger('onChanges').forSpreadsheet(ss).onChange().create();   //Line for creating the trigger based on changes that made in google sheet
            }
            else if (respond == ui.Button.CANCEL){    //If 'CANCEL' button is clicked will alert user no trigger installed
            ui.alert('No new trigger have been installed');
            }
            else { ui.alert('No new trigger have been installed')    //If close button is clicked will alert user no trigger installed
            }
    }
    function delTrigger(){    //Setup for deleting trigger
        var ui= SpreadsheetApp.getUi();
            try{
            var triggers = ScriptApp.getProjectTriggers();
            ScriptApp.deleteTrigger(triggers[0]);    //Choosing the trigger to be deleted
            ui.alert('Your trigger have been deleted');
               }
            catch(e){
               ui.alert('No trigger have been set!');    //If there is no trigger have been set, it will throw this alert to user.
            }
    }
    

    这一行应该替换我制作的代码中的当前触发器构建器。编辑器和您将需要单击任何自定义菜单一次以激活授权。

    ScriptApp.newTrigger('onChanges').forForm(key).onFormSubmit().create(); 
    

    上面这一行的解释:

    • 'onChange'是要触发的函数名
    • 'key' 是与电子表格绑定的表单的键/唯一 ID。

    希望这可以帮助您触发,如果这里有什么问题的话。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多