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.
Try this:
GS:
HTML:
Dialog:
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
HTML_Test.html