我想将Google脚本中的数据加载到HTML文档中并通过电子邮件发送。当我运行下面的代码时,出现错误ReferenceError: "document" is not defined.
。
我不了解错误的原因或解决方法。
我的GS脚本:
function sendEmail(){
//Email Template
var emailTemp = HtmlService.createTemplateFromFile("template"); // Access to the HTML document
// Tab Stichprobe
var stichprobe = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Stichprobe");
var stichprobecapital = stichprobe.getRange("A2:L"+stichprobe.getLastRow()).getDisplayValues(); // Array AssetName
var software = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Software");
var softwarecapital = software.getRange("A2:E"+software.getLastRow()).getDisplayValues();
var test=[];
var stichprobelen = stichprobe.getRange("A2:A"+stichprobe.getLastRow()).getDisplayValues().length;
var softwarelen = software.getRange("A2:A"+software.getLastRow()).getDisplayValues().length;
var softwareindex = [];
//Config
var config = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Config");
var subject = config.getRange("B1:B1").getValue();
var counter = config.getRange("B5:B5").getValue();
var subject = config.getRange("B1:B1").getValue();
var replyto = config.getRange("B3:B3").getValue();
var name = config.getRange("B2:B2").getValue();
var error = config.getRange("B6:B6").getValue();
var cc = config.getRange("B4:B4").getValue();
for (var a = 0; a<stichprobelen; a++){
for (var i = 0; i<softwarelen; i++){
if (softwarecapital[i][0] == stichprobecapital[a][0]){
softwareindex.push(i);
}
}
var softindexlen = softwareindex.length;
for(var b = 0; b < softindexlen; b++) {
var node = document.createElement("LI");
var textnode = document.createTextNode(softwarecapital[0][1]);
node.appendChild(textnode);
document.getElementById("liste").appendChild(node);
}
var htmlMessage = emailTemp.evaluate().getContent();
GmailApp.sendEmail(stichprobecapital[a][2], subject, error,{name:name, htmlBody: htmlMessage, replyTo: replyto, cc: cc});
}
}
我的HTML脚本:
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<ul id="liste">
</ul>
</body>
</html>
问候
答案 0 :(得分:1)
您正在尝试在服务器端代码中使用HTML DOM Document对象。导致错误的行是:
var node = document.createElement("LI");
您不能在Apps脚本“ .gs”文件中使用HTML DOM对象。您可以将Html Service-带脚本的HTML模板化:
https://developers.google.com/apps-script/guides/html/templates
您无法在浏览器中构建电子邮件的HTML,因为您没有加载Web应用程序,侧边栏或对话框。即使Apps脚本使用JavaScript,它也与您在浏览器中使用的不完全相同。而且DOM不是JavaScript。 DOM是允许在浏览器中操纵HTML的原因。
基本上,您需要使用文本串联构建列表的HTML。
答案 1 :(得分:0)
您正在Apps脚本上使用document
javascript property。
重要的是要了解Apps脚本不能与DOM模型一起运行。
您要做的是在前端(您的HTML文件)上有一个函数,从后端(Apps脚本文件)调用数据并填充数据。
为此,您将在HTML文件中使用client-side code和withSuccessHandler
来填充列表。这种处理方式符合best-practices。