本文介绍了未捕获的TypeError:google.script.run.doSomething不是函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试检查输入的名称是否已经在Google表格中.但是,我收到此错误:

I am trying to check if the input name is already in a Google Sheet. However, I am getting this error:

这是我的Index.html文件.

<!DOCTYPE html>
<html>

<head>
    <base target="_top">
    <meta charset="UTF-8">
</head>

<body>
    <input type="text" id="meetingTitle" value=""> // Getting value here
    <button onclick="checkName()">Check if available</button> //Calling function is is causing the error.
    <p id=nameVerification><i>Click the button above to check availability.</i></p>

    <script>
        function checkName() {
            var toPass = document.getElementById("meetingTitle").value;
            prompt("toPass " + toPass);
            google.script.run.doSomething();
        }

        function checkNameCS(checkNameSSReturn) {
            if (checkNameSSReturn == "") {
                document.getElementById('nameVerification').innerHTML = "Already in Use: Please try with another name."
                document.getElementById("meetingTitle").value = "";
            } else {
                document.getElementById("meetingTitle").value = checkNameSSReturn;
                document.getElementById('nameVerification').innerHTML = "Meeting name available. Procced."
            }


        }

        function doSomething () {
            var nameGiven = document.getElementById("meetingTitle").value;
            var nameExists = false;
            var nameVerified = false;
            var name = nameGiven.toLowerCase();
            name = strip(name);
            prompt("name " + name);


            var spreadsheetId = ''; //Sheet id entered
            var rangeName = 'Sheet1';
            var values = Sheets.Spreadsheets.Values.get(spreadsheetId, rangeName).values;
            if (!values) {} else {
                for (var row = 0; row < values.length; row++) {
                    if (name == values[row][0]) {
                        nameExists = true;
                    }
                }
            }

            if (nameExists) {
                checkNameCS("");
                prompt("name2 " + " ");
                return;
            }

            nameVerified = true;
            prompt("name2 " + name);
            checkNameCS(name);
            return;
        }

        function strip(str) {
             return str.replace(/^\s+|\s+$/g, '');
        }

    </script>
</body>

</html>

我尝试用提示进行调试,但没有成功.似乎函数正确地执行了某些操作.但是代码在google.script.run.doSomething();之后停止工作.

I tried debuging it with prompts but with no success. It seems like the function do something is properly called. But the code stops working aftergoogle.script.run.doSomething();.

我看过有关成功处理者的文档,但他们也不能解决问题.

I have looked at the documentation for successhandlers but they dont solve the issue either.

推荐答案

此修改如何?

    google.script.run.doSomething()中的
  • doSomething()必须是Google Apps脚本.
    • 在脚本中,doSomething()被放入HTML(index.html)中,并且包括使用Google Apps脚本的方法.运行google.script.run.doSomething()时,在Google Apps脚本(code.gs)上找不到doSomething().由此,发生这种错误.如果doSomething()在HTML端运行,则Sheets.Spreadsheets.Values.get()也会发生错误,因为Sheets.Spreadsheets.Values.get()是带有Google Apps脚本的高级Google服务方法.
    • doSomething() of google.script.run.doSomething() is required to be Google Apps Script.
      • In your script, doSomething() is put in HTML (index.html), and a method for using Google Apps Script is included. When google.script.run.doSomething() is run, doSomething() cannot be found at Google Apps Script (code.gs). By this, such error occurs. And if doSomething() is run at HTML side, also an error occurs at Sheets.Spreadsheets.Values.get(), because Sheets.Spreadsheets.Values.get() is the method of Advanced Google Services with Google Apps Script.

      在此修改中,您的脚本分为Google Apps脚本(code.gs)和HTML(index.html).在index.html中使用var nameGiven = document.getElementById("meetingTitle").value;checkNameCS(name);.

      In this modification, your script was separated to Google Apps Script (code.gs) and HTML (index.html). var nameGiven = document.getElementById("meetingTitle").value; and checkNameCS(name); are used in index.html.

      在运行此脚本之前,请在高级Google服务中启用Sheets API.

      By the way, before you run this script, please enable Sheets API at Advanced Google Services.

      function strip(str) {
        return str.replace(/^\s+|\s+$/g, '');
      }
      
      function doSomething (nameGiven) {
        var nameExists = false;
        var nameVerified = false;
        var name = nameGiven.toLowerCase();
        name = strip(name);
      
        var spreadsheetId = '###'; //Sheet id entered
        var rangeName = 'Sheet1';
        var values = Sheets.Spreadsheets.Values.get(spreadsheetId, rangeName).values;
        if (values) {
            for (var row = 0; row < values.length; row++) {
                if (name == values[row][0]) {
                    nameExists = true;
                }
            }
        }
      
        if (nameExists) {
            return "";
        }
      
        nameVerified = true;
        return name;
      }
      

      HTML:index.html

      <!DOCTYPE html>
      <html>
      
      <head>
          <base target="_top">
          <meta charset="UTF-8">
      </head>
      
      <body>
          <input type="text" id="meetingTitle" value="">
          <button onclick="checkName()">Check if available</button>
          <p id=nameVerification><i>Click the button above to check availability.</i></p>
      
          <script>
              function checkName() {
                  var toPass = document.getElementById("meetingTitle").value;
                  prompt("toPass " + toPass);
                  var nameGiven = document.getElementById("meetingTitle").value; // Added
                  google.script.run.withSuccessHandler(checkNameCS).doSomething(nameGiven); // Modified
              }
      
              function checkNameCS(checkNameSSReturn) {
                console.log(checkNameSSReturn)
                  if (checkNameSSReturn == "") {
                      document.getElementById('nameVerification').innerHTML = "Already in Use: Please try with another name."
                      document.getElementById("meetingTitle").value = "";
                  } else {
                      document.getElementById("meetingTitle").value = checkNameSSReturn;
                      document.getElementById('nameVerification').innerHTML = "Meeting name available. Procced."
                  }
              }
          </script>
      </body>
      
      </html>
      

      参考:

      • google.script.run类
      • Reference:

        • Class google.script.run
        • 这篇关于未捕获的TypeError:google.script.run.doSomething不是函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 18:20