简体   繁体   English

将Google Apps脚本实施为HTML

[英]Implement Google Apps Script to HTML

I want to call a Google sheet from within a HTML file. 我想从HTML文件中调用Google表格。 This is possible via documentation ( https://developers.google.com/apps-script/guides/html/templates ) by: 可以通过以下方式通过文档( https://developers.google.com/apps-script/guides/html/templates )进行以下操作:

<body>
    <? var data = SpreadsheetApp
        .openById('1234567890abcdefghijklmnopqrstuvwxyz')
        .getActiveSheet()
        .getDataRange()
        .getValues(); ?>    </body>

That really works and catches the values when I load the HTML File. 加载HTML文件时,它确实有效并捕获了值。 What I need to do is - I want to set values in a Spreadsheet within a for-loop, which is activated by a button. 我需要做的是-我想在for循环内的Spreadsheet中设置值,该循环由一个按钮激活。

function processForm() {

var elementsNr = document.getElementsByName("nummerierung")
var elements = document.getElementsByName("schuelername")
var elementsDate = document.getElementsByName("datuminput")
var elementsArt = document.getElementsByName("art")
var elementsZeit = document.getElementsByName("zeit")

for (var i = 0; i < elements.length; i++) {
    var Nr = elementsNr[i].value;
    var sname = elements[i].value;
    var datum = elementsDate[i].value;
    var Uart = elementsArt[i].value;
    var UZeit = elementsZeit[i].value;

     var data = SpreadsheetApp
                   .openById('1MWy0J5ZKnsXXKWEX72Bh9cRBvsFkDAqTEhW1SxFavmA')
                   .getActiveSheet()
                   .getDataRange(sheet.getLastRow()+1, 1, 1, 5)
                   .getValues([[Nr, datum, sname, Uart, UZeit]]); 
}

But somehow I get an reference Error: 但是以某种方式我得到参考错误:

ReferenceError: SpreadsheetApp is not defined ReferenceError:未定义SpreadsheetApp

:( :(

Anyone? 任何人?

The referred documentation is about Templated HTML but the code shown in the question doesn't use the templated HTML syntax. 参考的文档是关于模板HTML的,但是问题中显示的代码未使用模板HTML语法。 By the other hand, the Best Practices doc suggests to load data asynchronally. 另一方面, 最佳做法文档建议异步加载数据。

To call a server-side Apps Scripts functions from the client-side JavaScript (scripts on an HTML file), first create a function on the .gs file, then call it from the HTML by using google.script.run 要从客户端JavaScript(HTML文件上的脚本)调用服务器端Apps脚本函数,请首先在.gs文件上创建一个函数,然后使用google.script.run从HTML调用它

For a code example see the answer by Daniel to SpreadsheetApp.getActiveSpreadsheet() is breaking script 有关代码示例,请参见DanielSpreadsheetApp回答 。getActiveSpreadsheet()破坏了脚本

By the way, getDataRange and getValues should not have arguments. 顺便说一句, getDataRangegetValues应该没有参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM