简体   繁体   中英

Implement Google Apps Script to HTML

I want to call a Google sheet from within a HTML file. This is possible via documentation ( https://developers.google.com/apps-script/guides/html/templates ) by:

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

That really works and catches the values when I load the HTML File. What I need to do is - I want to set values in a Spreadsheet within a for-loop, which is activated by a button.

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

:(

Anyone?

The referred documentation is about Templated HTML but the code shown in the question doesn't use the templated HTML syntax. 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

For a code example see the answer by Daniel to SpreadsheetApp.getActiveSpreadsheet() is breaking script

By the way, getDataRange and getValues should not have arguments.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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