[英]Global variable not updating as expected
I am building a small tool using Apps Script.我正在使用 Apps 脚本构建一个小工具。 I can't seem to get the global variable (URL) to update to include the docId.我似乎无法获取要更新的全局变量 (URL) 以包含 docId。
What I expect我期待什么
What is happening:怎么了:
myClickHandler
is not updated to the value of 'docId' set in the createProject function. myClickHandler
中的变量“url”未更新为在 createProject function 中设置的“docId”值。var docId = "";
var url = "https://docs.google.com/document/d/" + docId + "/";
function doGet() {
var output = HtmlService.createHtmlOutputFromFile('MyUI');
return output;
}
function createProject(test="test"){
const doc = DocumentApp.create(test);
docId = doc.getId();// <-- Update global docId variable. Doesn't seem to be working.
Logger.log("Doc ID is: " + docId);
}
function myOnClickHandler(){
const myList = ["Hello from the server", url]
Logger.log("URL is: " + url);
}
function app(){
createProject();
myOnClickHandler();
}
Probably a simple mistake on my part, but I am banging my head against the wall on this one.对我来说可能是一个简单的错误,但我正在用头撞墙。 Screenshot attached.附上截图。
In JavaScript, strings are immutable .在 JavaScript 中,字符串是不可变的。 Use an arrow function and a template literal instead of a static text string, like this:使用箭头 function和模板文字代替 static 文本字符串,如下所示:
let docId;
const getUrl_ = (docId) => `https://docs.google.com/document/d/${docId}/`;
function test() {
docId = 'testString';
const url = getUrl_(docId);
console.log(url);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.