c_ph_r
c_ph_r

Reputation: 267

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?

Upvotes: 0

Views: 212

Answers (1)

Wicket
Wicket

Reputation: 38150

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.

Upvotes: 1

Related Questions