I have a function for web applications that can copy templates. It receives the name of the template and the id of the template as parameters.
code.gs
Function generate_(idTemplate, name) {
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) // 这些值是我想要传递给客户端的。 }
This function is what I use to pass the copied name and URL to the client. I know that in JavaScript, to return multiple parameters, it must be implemented through an array.
function getFile(nameF,linkF){ var array = [nameF,linkF]; return array; }
This is the client script I'm using to try to retrieve the generated replicated data:
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>
I use a button to test whether it is passed correctly, but I cannot display the data, the browser console shows null when displaying the log.
What could I have done wrong? On the client side I've tried google.script.run.withSuccessHandler(copyValues).getFile(nameF,linkF);
but it didn't work.
<button id="btn" onclick="create(); getValues();"
This is the button that triggers create()
, a script to create the copy, and getValues();
, to get the name and URL of that copy. The template copy is created successfully, but the filename and URL are not retrieved on the client.
P粉1470452742023-09-19 13:18:54
Try this:
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:
P粉9519143812023-09-19 10:15:01
This is an example of getting file information.
As shown in the screenshot below, when I click the button, the file name and URL will be displayed in the input box.
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>