在 html 中使用 googlescript api

Use googlescript api in html

我有一个 html 文件绑定到一个 google spreadsheet,它创建了一个小的图形用户界面。 gui上有按钮,我想用按钮做涉及spreadsheet的事情,所以我想调用SpreadsheetApp.

之类的东西

例如,为了让我了解如何正确执行此操作,假设我只想更改跨页 sheet 中的一个单元格,或者将一个值记录到记录器中。这是提供的 html 文件。

<div>
<P>Select an option below</P>
<script type="text/javascript">
Logger.log("in html");
SpreadsheetApp.getActiveSheet().getRange(2, 14).setValue('In HTML');
</script>
</div>

我们假设这是服务于 html

的 google 脚本
function openDialog() {
 var html = HtmlService.createHtmlOutputFromFile('TeacherView')
  .setSandboxMode(HtmlService.SandboxMode.IFRAME);
 SpreadsheetApp.getActive().show(html);

}

当 html 文件运行时,我不应该看到 spreadsheet 中的范围更改值吗?或者看到记录器记录一个值?我是不是误解了脚本和 html 的组合是如何工作的?

对于一起创建 html/javascript 事物有点陌生,因此非常感谢您的帮助。

您必须使用:

google.script.run.nameOfServerFunction()

https://developers.google.com/apps-script/guides/html/reference/run

您不能在 HTML 的脚本标记中直接使用 Apps 脚本服务。

<div>
<P>Select an option below</P>
<script type="text/javascript">
  console.log("in html");

  function onSuccess(argReturnValue) {
    alert('Success ' + argReturnValue);
  }

  var dataToPassToServer = get your data;

  google.script.run.withSuccessHandler(onSuccess)
    .fncMyGsScriptFunctionNameHere(dataToPassToServer);

</script>
</div>

Code.gs

function fncMyGsScriptFunctionNameHere(argDataPassedInHere) {
  SpreadsheetApp.getActiveSheet().getRange(2, 14).setValue(argDataPassedInHere);
};