javascript可以读取从脚本函数返回的字符串吗? [英] can javascript read strings returned from app script functions?

查看:105
本文介绍了javascript可以读取从脚本函数返回的字符串吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Google-Apps-Script中,我创建了一个函数,它返回一个包含电子表格文件ID的字符串(该ID因用户而异,因为它是从模板复制的新文件)。

In Google-Apps-Script I have created a function which returns a string containing the id of a spreadsheet file (this id will vary from user to user, as it is a new file copied from a template).

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变量时,它变得未定义。

The Logger.log(newID) shows the id name of the file in my testing. However when I try to initiate a javascript variable to that returning id string it becomes undefined.

<script>
var idname;
idname = google.script.run.findSheet();
alert(idname);

我有另一个谷歌脚本应用程序函数(从javascript调用),它使用该id字符串变量作为一个参数,以便将用户输入从我的html页面写入他们的Google电子表格中,作为Web应用程序的一部分,但是来自javascript的id名称未定义,并且该函数不起作用。

I have another google scripts app function (which is called from javascript )which uses that id string variable as a parameter in order to write user input from my html page into their google spreadsheet as part as a web app, but the id name from the javascript goes in as undefined and the function does not work.

有什么我不知道为什么应用程序脚本字符串在javascript中调用时未定义?

Is there something I do not know on why the apps script string is undefined when called on by javascript?

我尝试使用idname.toString()修复,但它仍然显示为未定义当我使用警报(idname)或document.write(idname)或重写段落

I tried using idname.toString() to fix but it still shows as undefined when i use an alert(idname) or document.write(idname) or rewriting a paragraph

<p idname="showmeID"></p>
<script>
document.showmeID.innerHTML.idname


推荐答案


google.script.run是异步客户端JavaScript API,可用于HTML服务页面,可调用服务器端Apps脚本函数。

google.script.run is an asynchronous client-side JavaScript API available in HTML-service pages that can call server-side Apps Script functions.

根据文档此API是异步的,不会直接返回值。

According to the documentation this API is asynchronous and does not return the value directly. Instead it returns the value in a callback function.

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可以读取从脚本函数返回的字符串吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆