首页 > 解决方案 > google.script.run 错误,我该如何解决?

问题描述

谷歌脚本代码

嗨,伙计们,我在使用基本代码时遇到了困难,一旦我添加了 google.scripts.run,它似乎就停止了工作

您的意见将不胜感激

下面的代码是我的谷歌脚本代码“Code.gs”

function AddRecord(name) {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var mainSheet = ss.getSheetByName("Main");
  mainSheet.appendRow([name,new Date()]);
 

}
function startForm()
{
  var form = HtmlService.createHtmlOutputFromFile('AddForm');
  SpreadsheetApp.getUi().showModalDialog(form,'Add Record');
  
}

function addMenu(){

  var menu = SpreadsheetApp.getUi().createMenu('Custom');
  menu.addItem('Add Record Form', 'startForm');
  menu.addToUi();
}

function onOpen(e){
  addMenu();
}

下面的代码是我的 HTML “AddForm.HTML”

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
    <script>
    function AddRow(){
    var name = document.getElementById("name").value;
    google.script.run.AddRecord(name);
    }
    </script>
  </head>
  <body>
    Name:&nbsp;&nbsp;<input type="text" id="name" />
    <input type="button" value="Add" onclick="google.script.run.AddRecord(name);" />
  </body>
</html>

标签: javascriptgoogle-apps-scriptgoogle-sheets

解决方案


您的代码似乎有点错误。

查看该AddForm文件,您已经创建了一个名为的函数,该函数AddRow使用google.script.run并且这是一个很好的继续操作的方法。但是你从来没有真正使用过这个函数本身。

在按钮中,您有:

<input type="button" value="Add" onclick="google.script.run.AddRecord(name);" />

但该变量name仅在AddRow.

将其更改onClick为实际调用AddRow,您应该会很好。

修改代码

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
    <script>
    function AddRow(){
    var name = document.getElementById("name").value;
    google.script.run.AddRecord(name);
    }
    </script>
  </head>
  <body>
    Name:&nbsp;&nbsp;<input type="text" id="name" />
    <input type="button" value="Add" onclick="AddRow();" />
  </body>
</html>

推荐阅读