未捕获的TypeError:google.script.run.doSomething不是函数

时间:2019-05-26 06:13:55

标签: javascript google-apps-script web-applications google-sheets server-side

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

  

未捕获的TypeError:google.script.run.doSomething不是函数。

这是我的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();之后停止工作。

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

1 个答案:

答案 0 :(得分:0)

此修改如何?

您的脚本的问题:

    doSomething()中的
  • google.script.run.doSomething()是Google Apps脚本。
    • 在您的脚本中,doSomething()以HTML(index.html)的形式出现,并且包括使用Google Apps脚本的方法。运行google.script.run.doSomething()时,无法在Google Apps脚本(doSomething())上找到code.gs。由此,发生这种错误。并且,如果doSomething()在HTML端运行,则Sheets.Spreadsheets.Values.get()也会发生错误,因为Sheets.Spreadsheets.Values.get()是使用Google Apps Script的高级Google服务的方法。
  • 如果将其放入Google Apps脚本(code.gs),则需要修改doSomething()脚本中使用的Javascript。

修改后的脚本:

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

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

Google Apps脚本:code.gs
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>

参考: