简体   繁体   English

Google Apps for Forms脚本:希望通过电子邮件向用户发送特定号码

[英]Google Apps Script for Forms: Want to send users specific number on email

It is possible to ask users to write their email addresses in FORM and after that they recieve email with unique number? 可以要求用户在FORM中写出他们的电子邮件地址,然后他们收到具有唯一编号的电子邮件吗?

I find out how to send automatic email to submitter here - http://www.labnol.org/internet/auto-confirmation-emails/28386/ 我在这里了解如何发送自动电子邮件给提交者-http: //www.labnol.org/internet/auto-confirmation-emails/28386/

But don't know how to send to him unique number each! 但是不知道如何分别给他发送唯一的号码!

you could try the above code to retrieve the unique id of the form response. 您可以尝试使用上面的代码来检索表单响应的唯一ID。 But it's not sorted numbers(1,2,...). 但这不是排序的数字(1,2,...)。 It's a unique random number generated by the form. 这是表单生成的唯一随机数。 my advice is not to try to build something starting to 1 and going further, as it's assynchronious you do not have the security to to have a sorted list of items. 我的建议是不要尝试构建从1开始甚至更远的东西,因为它是异步的,因此您没有安全的排序项目列表。 see here for details. 详情请参阅这里

/**
* for a form with 2 question named "question 1" and "question 2"
* this function need to be associated directly to the form
* and it need to be associated to a trigger "on form submit"
* the "e" given as argument is generated by the form submit trigger
*/
function submitFormFunc(e) {
  var items = e.response.getItemResponses();
  var responses={};
  for(var i = 0; i< items.length; i++) {
   responses[items[i].getItem().getTitle()]=items[i].getResponse();
  }

  var responseTable = [];
  var responseIndex = ["Timestamp","ID","question 1","question 2"];
  responseTable.push(e.response.getTimestamp().toString());
  responseTable.push(e.response.getId());
  responseTable.push(responses["question 1"]); // my form have 2 questions named "question 1" and "question 2"
  responseTable.push(responses["question 2"]);
  responseTable.push(FormApp.getActiveForm().getResponse(e.response.getId()).getEditResponseUrl());
  SpreadsheetApp.openById("YOUR_SPREADSHEET_ID").appendRow(responseTable);
}

--EDIT-- - 编辑 -

if You want something shorter you can use the time stamp: 如果您想缩短时间,可以使用时间戳:

change (or add) responseTable.push(e.response.getTimestamp().toString()); 更改(或添加) responseTable.push(e.response.getTimestamp().toString()); for 对于

responseTable.push(e.response.getTimestamp().getTime().toString());

or even shorter but a little less efficient: 甚至更短,但效率稍低:

you can add a line: 您可以添加一行:

responseTable.push(generateUnique());

and add the function: 并添加功能:

function generateUnique(){
  var vals =  SpreadsheetApp.openById("YOUR_SPREADSHEET_ID").getRange("A:A").getValues();
  var ids=[];
  for(var i in vals){
    ids.push(vals[0]);
  }
  var newId = Math.floor(Math.random()*100); // change 100 for something bigger if needed
  while(ids.indexOf(newId)>-1){
    newId = Math.floor(Math.random()*100);
  }
  Logger.log(newId);
  return(newId);
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM