在 Google-Apps-Script 中,我创建了一个函数,它返回一个包含电子表格文件 ID 的字符串(此 ID 因用户而异,因为它是从模板复制的新文件)。
function findSheet(){
//Opens User's Google Drive and searches files for a spreadsheet called "blahblah" and returns the active spreadsheet
var files = DriveApp.getFilesByName("blahblah");
var file = files.next();
var newID = file.getId();
Logger.log(newID);
return newID;
}
Logger.log(newID) 在我的测试中显示文件的 ID 名称。但是,当我尝试为返回的 id 字符串启动一个 javascript 变量时,它变得未定义。
<script>
var idname;
idname = google.script.run.findSheet();
alert(idname);
我有另一个 google scripts app 函数(从 javascript 调用),它使用该 id 字符串变量作为参数,以便将用户输入从我的 html 页面写入他们的 google 电子表格作为网络应用程序的一部分,但是 id来自 javascript 的名称未定义,函数不起作用。
有什么我不知道为什么应用程序脚本字符串在被 javascript 调用时未定义的原因吗?
我尝试使用 idname.toString() 来修复,但当我使用 alert(idname) 或 document.write(idname) 或重写段落时它仍然显示为未定义
<p idname="showmeID"></p>
<script>
document.showmeID.innerHTML.idname
最佳答案
google.script.run is an asynchronous client-side JavaScript API available in HTML-service pages that can call server-side Apps Script functions.
根据documentation此 API 是异步的,不会直接返回值。相反,它会在回调函数中返回值。
google.script.run
.withFailureHandler(function(err){
// callback function that will be executed when some error will occur.
// Error object is passed as the first argument.
console.log("failure handler", err)
})
.withSuccessHandler(function(res){
// here withSuccessHandler runs when your findSheet function runs successfully.
// value returned from google app script will be passed as the first argument of this callback function.
// So you will receive the value returned from GAS in variable `res`.
console.log("response from findSheet function", res)
})
.findSheet();
关于javascript - javascript 可以读取应用程序脚本函数返回的字符串吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38964058/