未捕获的TypeError:google.script.run。不是一个功能

时间:2017-05-30 16:53:30

标签: javascript google-apps-script google-sheets google-apps

我有一个与Google电子表格相关联的代码,可通过脚本编辑器访问。在代码中,我有一个.gs文件作为主体,我有一个HTML文件,它是电子表格中使用的侧边栏。我正在尝试从HTML编辑器中调用sheet.gs文件中的函数,我是否会使用HTML文件中的“google.script.run.myFunction()”执行此操作,我这样做错了吗?

基本上在我的sheet.gs中我有

function myFunction() {
    // do a thing
}

在我的HTML文件中,我试图像myFunction()一样致电google.script.run.myFunction()

请帮我解决这个问题,谢谢! 编辑: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~

我有这个按下按钮

调用的功能
    <div class="btn" onclick="generateSpreadsheetReport()">View Responses</div>

,功能是

        function generateSpreadsheetReport() {
        console.log("RUN!");
        console.log("StudentAverage1");
        var day = document.getElementById("day");
        if (!day.value) day.value = "";
        var course = document.getElementById("courses");
        var stud = document.getElementById("stud");
        console.log(day.value,course.value,stud.value)
        google.script.run.withSuccessHandler(displayAverage).sortByParameter(day.value||"", course.value||"", stud.value||"");
        console.log("StudentAverage2");
      google.script.run.getStudentAverage(); // <----- This line right here isn't working.
    }

然后在我的sheet.gs中我有一个名为getStudentAverage()的函数,它被定义,当我从资源页面点击它时,它会显示在库中。所以我假设我在HTML

中称错了

2 个答案:

答案 0 :(得分:0)

看看下面的基本示例。单击该按钮可触发电子表格中的Toast。随意复制文档并检查代码

https://docs.google.com/spreadsheets/d/199k9x1Sn_NQJlNwB5JDZ_fgozEY0fFMYiXBiPHOQsR8/edit?usp=sharing

如果这不起作用,请分享更多代码。

答案 1 :(得分:0)

不确定您的问题是什么,但从您sheet.gs尝试更改

的外观来看
myFunction() {
    // do a thing
}

function myFunction() {
    return true;
}

根据您所解释的问题,这只是一个疯狂的猜测。试试这个并告诉我们它是否适合您。

来自HTML文件的

更新调用{/ 1}}

myFunction()