谷歌脚本项目触发器没有运行? [英] Google script project trigger not running?

查看:107
本文介绍了谷歌脚本项目触发器没有运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Javascript非常陌生,并且一直在研究此脚本以在电子表格中创建最新条目(从Google表单创建),将收集的用户电子邮件地址与第二张表中的名单进行匹配,并发送电子邮件给家长。我是一名老师,想法是创建一个Google表单,用于编辑学生输入的信息,并在提交表单/更新表单时将其发送给父母。



我知道这很麻烦......还有一些额外的变量和事情,但是当你运行脚本时,脚本可以完美/按预期工作。唯一的是,我试图在提交表单时让脚本在触发器上运行,但事实并非如此。我缺少使用触发器的东西吗?



代码如下:

  function createEmail(){
//为这两个表单设置变量
var ss = SpreadsheetApp.getActiveSpreadsheet();

var sheet1 = ss.getSheets()[0];
var sheet2 = ss.getSheets()[1];

//这将从最近的条目中收集信息并将其写入一个名为newReflectionValues的数组
var reflectionLastRow = sheet1.getLastRow();
var reflectionLastColumn = sheet1.getLastColumn();
var reflectionLastCell = sheet1.getRange(reflectionLastRow,reflectionLastColumn).getValue();
var reflectionRange = sheet1.getRange(reflectionLastRow,1,1,reflectionLastColumn);
var newReflectionValues = reflectionRange.getValues();

var studentEmail = newReflectionValues [0] [3];

Logger.log(NEW REFLECTION VALUES)
Logger.log(newReflectionValues);

Logger.log(电子邮件将发送给学生电子邮件:)
Logger.log(studentEmail)

//创建父邮件地址的数组
var rosterLastRow = sheet2.getLastRow();
var rosterLastColumn = sheet2.getLastColumn();
var rosterEmails = sheet2.getSheetValues(2,1,rosterLastRow,rosterLastColumn);

Logger.log(家长电子邮件)
Logger.log(rosterEmails);

//交叉检查电子邮件 - 如果匹配,将电子邮件写入变量
var parentEntriesLength = rosterLastRow;

for(i = 0; i< parentEntriesLength; i ++){
var currentRange = rosterEmails [i]; (currentRange [2] == studentEmail){
var toParents = String(currentRange [3])+,+ String(currentRange [4]);


var studentName = String(currentRange [0]);
var countOfReflections = currentRange [6];
休息;
} else {
var toParents =未找到父母电子邮件;



//完成电子邮件下面

MailApp.sendEmail({
to:toParents,
bcc: rdoyle@rafos.org+,+ String(studentEmail),
主题:行为反映通知,

htmlBody:< p> Hello,< / p> ;+
< p>今天studentName收到以下操作的行为反映:< / p>+
< p> newReflectionValues< / p>+
< p>他们在课堂上短暂休息并完成以下反思:< / p>+
< p> reflectionInformation< / p>+
< p> + String(studentName)+已收到+ countOfReflections +今年的反映+< / p>+
< p>此邮件已发送,如果您对此行为或事件有任何疑问,请随时询问。< / p>

});



解决方案

你是意识到有两种类型的触发器



当您点击保存时,您应该被要求授权,如果您没有被要求,请点击调试/运行按钮来授权脚本。



如果它仍然不起作用,请在视图中检查执行脚本 - >执行脚本,最后一行将指示错误。 / p>

I'm pretty new to Javascript and have been working on this script to take the most recent entry in a spreadsheet (created from a Google form), match the users email address that is collected to a roster in a second sheet, and send an email to parents. I'm a teacher and the idea is to be able to create a google form that compiles the info students enter and email it to their parents once they submit the form/update the sheet.

I know it's pretty messy...there are some extra variables and things, but the script works perfectly/as expected when you "Run" the script. The only thing is, I have tried to have the script run on a trigger when the form is submitted, but it doesn't. Am I missing something with using triggers?

Code is below:

function createEmail() {
  // Sets variables for both sheets
  var ss = SpreadsheetApp.getActiveSpreadsheet();

  var sheet1 = ss.getSheets()[0];
  var sheet2 = ss.getSheets()[1];

  // This gathers information from the most recent entry and write it to an array called newReflectionValues
  var reflectionLastRow = sheet1.getLastRow();
  var reflectionLastColumn = sheet1.getLastColumn();
  var reflectionLastCell = sheet1.getRange(reflectionLastRow, reflectionLastColumn).getValue();
  var reflectionRange = sheet1.getRange(reflectionLastRow, 1, 1, reflectionLastColumn);  
  var newReflectionValues = reflectionRange.getValues();

  var studentEmail = newReflectionValues[0][3];

    Logger.log("NEW REFLECTION VALUES")
    Logger.log(newReflectionValues);

    Logger.log("Email will send to student email:")
    Logger.log(studentEmail)

  // Makes an array of the parent email addresses
  var rosterLastRow = sheet2.getLastRow();
  var rosterLastColumn = sheet2.getLastColumn();
  var rosterEmails = sheet2.getSheetValues(2, 1, rosterLastRow, rosterLastColumn);

  Logger.log("PARENT EMAILS")
  Logger.log(rosterEmails);  

  // Cross check emails - if a match, write emails to variable
  var parentEntriesLength = rosterLastRow;

  for (i = 0; i < parentEntriesLength; i++) {
    var currentRange = rosterEmails[i];

    if (currentRange[2] == studentEmail) {
      var toParents = String(currentRange[3]) + ", " + String(currentRange[4]);
      var studentName = String(currentRange[0]);
      var countOfReflections = currentRange[6];
      break;
    } else {
    var toParents = "NO PARENT EMAILS FOUND";
    }
  } 

// FINISH EMAIL BELOW

  MailApp.sendEmail({
     to: toParents,
     bcc: "rdoyle@rafos.org" + ", " + String(studentEmail),
     subject: "Behavior Reflection Notification",

    htmlBody: "<p>Hello,</p>" +
    "<p>Today studentName received a behavior reflection for the following action:</p>" +
    "<p>newReflectionValues</p>" +
    "<p>They took a short break in class and completed the following reflection:</p>" +
    "<p>reflectionInformation</p>" +
   "<p>" + String(studentName) + " has recieved " + countOfReflections + " reflections this year." + "</p>" +
    "<p>This email has been sent with information that the student completed directly on the reflection form and has been bcc'd to them as well as myself. If you have any questions regarding this behavior or incident, please feel free to ask.</p>"

  });

}

解决方案

You are aware that there are 2 types of trigger simple and installable but I think you are a little confused as to what they actually mean/ do. I'll try to explain the key points from documentation here.

A simple trigger is used by simply naming the function with the trigger name. For example, with Sheets / Forms, the trigger onFormSubmit(e) is fired when the user submits a form. The parameter e contains all the information relating to the submission, you should look into this as it's much more reliable than your current method of getting the submitted information. See here: 'e' parameter

Simple triggers are limited in their functionality since the script doesn't have to be authorised for the trigger to fire. A simple trigger cannot access other files, send emails or perform any action that requires authorisation. See here

An installed trigger is one that is either manually set up by the user or a script. Installed triggers have a lot more functionality but they still have some restrictions. See here

An installed trigger can call any named function and e parameter works in the same way as it does with simple triggers.

From your code above your installed trigger should look like this.

When you click save you should be asked for authorisation, if you are not asked, click the debug/ run button to authorise the script.

If it still doesn't work check the execution transcript in view -> execution transcript, the last line will indicate the error.

这篇关于谷歌脚本项目触发器没有运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆