我有一个脚本可以从面板中捕获信息。输入信息后,用户单击“提交”按钮,输入的数据将存储在电子表格中,该电子表格稍后将用于替换我在该电子表格上的单元格上的模板的某些字段。这个过程非常耗时,比如大约一分钟就完成了这一切。我想知道的是,如果可以使用参数信息,而不是将数据存储在电子表格的单元格中,而不是将数据存储在我拥有的模板上。关于如何做到这一点的任何想法?
function a(e) {
var doc = SpreadsheetApp.getActiveSpreadsheet();
var timecell = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Templates").getRange("A2");
var timebval = timecell.setValue(e.parameter.Times);
var t1 = timecell.getValue();
var mincell = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Templates").getRange("A3");
var minbval = mincell.setValue(e.parameter.Minutes);
var t2 = mincell.getValue();
var namecell = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Templates").getRange("A4");
var nabval = namecell.setValue(e.parameter.Name);
var t3 = namecell.getValue();
var email = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Templates").getRange("A5");
var emaval = email.setValue(e.parameter.email);
var t4 = email.getValue();
var address = "albdominguez25@gmail.com";
var advancedArgs = {bcc:t4};
var emailSubject = "Test";
var emailTemplate =SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Templates").getRange("A1").getValue() ;
emailTemplate = emailTemplate.replace("TIME",t1).replace("MIN",t2).replace("EXP",t3);
MailApp.sendEmail(address, emailSubject, emailTemplate, advancedArgs);
Browser.msgBox("Your Email has been sent!");
答案 0 :(得分:3)
不是将某些内容存储到电子表格中并从中读取每个值,而是直接使用replace方法中的值,如下所示。
function a(e) {
var address = e.parameter.email;
var advancedArgs = {bcc:t4};
var emailSubject = "Test";
var emailTemplate = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Templates").getRange("A1").getValue() ;
emailTemplate = emailTemplate.replace("TIME", e.parameter.Times).replace("MIN", e.parameter.Minutes).replace("EXP", e.parameter.Name);
MailApp.sendEmail(address, emailSubject, emailTemplate, advancedArgs);
Browser.msgBox("Your Email has been sent!");
}
如果您必须将这些值存储在电子表格中以供日后参考,则执行一次setValues而不是多次调用setValue会更快。使用参数信息发送电子邮件后,只需调用此选项即可立即设置所有值:
var range = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Templates").getRange("A2:A5");
range.setValues([[e.parameter.Times], [e.parameter.Minutes], [e.parameter.Name], [e.parameter.email]]);