我有一个用于网页应用的函数,可以复制模板,它接收模板的名称和模板的id作为参数。
代码.gs
函数generate_(idTemplate, 名称) {
var template = DriveApp.getFileById(idTemplate);
var folder = '文件夹id';
var copyName = name + "-" + CurrentDate;// 添加创建日期
var copy;
try {
copy = template.makeCopy(copyName, folder);
} catch (e) {
Logger.log(e.stack);
}
var nameF = copy.getName();
var linkF = copy.getUrl()
Logger.log(nameF,linkF)
return getFile(nameF,linkF) // 这些值是我想要传递给客户端的。
}
这个函数是我用来将复制的名称和URL传递给客户端的。我知道在JavaScript中要返回多个参数,必须通过数组来实现。
function getFile(nameF,linkF){
var array = [nameF,linkF];
return array;
}
这是我用来尝试检索生成的复制数据的客户端脚本:
HTML
<script>
function getValues(){
google.script.run.withSuccessHandler(copyValues).getFile();
}
function copyValues(values){
var nameF = values[0];
var urlF = values[1];
console.log(values);
console.log(nameF);
console.log(urlF);
console.log("值成功传递了");
}
</script>
我使用一个按钮来测试是否正确传递,但是我无法显示这些数据,浏览器控制台显示日志时显示null。
我可能做错了什么?在客户端我已经尝试了google.script.run.withSuccessHandler(copyValues).getFile(nameF,linkF);,但没有起作用。
<button id="btn" onclick="create(); getValues();"
这是按钮,它触发了create(),用于创建复制的脚本,以及getValues();,用于获取该复制的名称和URL。模板复制成功创建,但文件名和URL未在客户端检索到。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
试试这个:
GS:
function launchmydialog() { let t = HtmlService.createTemplateFromFile("ah2"); t.t1 = "one"; t.t2 = "two"; SpreadsheetApp.getUi().showModelessDialog(t.evaluate(),"Dialog"); }HTML:
<!DOCTYPE html> <html> <head> <base target="_top"> </head> <body> <input type="text" id="txt1" name="text1" value="= t1 ?>" /><br> <input type="text" id="txt2" name="text2" value="= t2 ?>" /><br> </body> </html>Dialog:
这是一个获取文件信息的示例。
如下截图所示,当我点击按钮时,文件名和URL将显示在输入框中。
Code.gs
function showTest() { setFileInfo(); let html = HtmlService.createHtmlOutputFromFile("HTML_Test"); SpreadsheetApp.getUi().showModalDialog(html,"Test"); } function setFileInfo() { let id = "1xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxE"; let name = "Copy of Template"; let template = DriveApp.getFileById(id); let copy = template.makeCopy(name); let properties = PropertiesService.getDocumentProperties(); let property = [copy.getName(),copy.getUrl()]; properties.setProperty("FileInfo",JSON.stringify(property)); } function getFileInfo() { let properties = PropertiesService.getDocumentProperties(); let property = properties.getProperty("FileInfo"); if( property ) { return JSON.parse(property); } return "Error"; }HTML_Test.html
<!DOCTYPE html> <html> <head> </head> <body> <input id="getFileInfoButton" type="button" onclick="getFileInfoClicked()" value="获取文件信息"><br> <input id="getFileInfoText" type="text"> <script> function getFileInfoClicked() { try { google.script.run.withSuccessHandler( function (value) { document.getElementById("getFileInfoText").value = value; } ).getFileInfo(); } catch(err) { alert(err); } } </script> </body> </html>