【问题标题】:HTML/JS Served by Google Apps Script由 Google Apps 脚本提供的 HTML/JS
【发布时间】:2020-07-27 13:25:17
【问题描述】:

我最近使用谷歌应用脚​​本启动了一个项目,我对这种语言是全新的。这是一个有点愚蠢的问题,但我找不到答案。在该工具中,我提供了一个 HTML 表单,稍后我计划使用它来创建一个单独的电子表格。

function doGet(){
var html = HtmlService.createHtmlOutputFromFile('form');
}

form.html

<head>
<script></script>
</head>
<body>
   ...content...
</body>

我对这里使用的语言有疑问。当我尝试在脚本标签中编写内容时,我是使用 Javascript 编写还是使用 Google Apps 脚本编写?更具体地说,我是否可以像在 Google Apps 脚本中那样访问 SpreadsheetApp 类?

【问题讨论】:

    标签: javascript html google-apps-script google-sheets


    【解决方案1】:

    客户端脚本是纯 JavaScript。您无法访问像 SpreadsheetApp 这样的 Google 库。您确实可以访问 google.script.* 方法,但没有其他权限。

    【讨论】:

      【解决方案2】:

      在您的 Apps Script Webapp 中使用 Apps Script 脚本时,您可以使用 Apps Script 方法和语法

      您可以像在 Code.gs 文件中一样调用 Apps Script 服务(例如 SpreadsheetApp)

      示例:

      <head>
      <script></script>
      </head>
      <body>
      The value from cell A1 in sheet "Sheet 1" is:  <?= SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet 1").getRange("A1").getValue() ?>.
        </body>
      

      有关 Apps Script WebApps 和 scriptlet herehere 的更多信息。

      注意:

      要使用scriptlet,您需要创建templateevaluate,如果您想在客户端可视化它,还需要return html 输出。

      doGet() 函数的示例:

      function doGet(){
        var html = HtmlService.createTemplateFromFile('form').evaluate();
        return html;
      }
      

      还有:

      确保您的脚本具有必要的 scopes 来执行您在 html 文件中实现的请求,例如“https://www.googleapis.com/auth/spreadsheets.readonly”。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-04-09
        • 1970-01-01
        • 2021-05-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多