未捕获的 TypeError 错误:google.script.run. 不是一个函数。

4
我有一个与谷歌电子表格相关的代码,可以通过脚本编辑器访问。在代码中,我有一个 .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 中调用它的方式不正确。

1个回答

0

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

myFunction() {
    // do a thing
}

function myFunction() {
    return true;
}

根据您所解释的问题,这只是一个猜测。请尝试这个方法并告诉我们是否适用于您。

更新:从您的HTML文件中调用myFunction(),如下所示:

function myFunction() {
        return true;
}

google.script.run.withSuccessHandler(function(myparam){
   console.log("Response: "+myparam);
}).myFunction();

抱歉,在我的实际代码中,我有一个名为myFunction()的函数{}我会更新我的问题。 - Gwinert
请按照以下步骤实现 sortByParameter(),将 google.script.run.getStudentAverage(); 更改为 google.script.run.withSuccessHandler().getStudentAverage();。请参阅文档 - Class google.script.run (Client-side API) 以获取代码实现和指南。希望这能有所帮助。 - Mr.Rebot

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接