1

with Google Script i have created this html that send datas to a google sheet taking in input some data from the url (after /exec?P=tonno&Q=3&C=Cliente+1 he takes in input P,Q,C):

<!DOCTYPE html>
<html>
  <head>
    <title> Modulo di Ordine </title>
  <script type="text/javascript">
    function doGet(e){ 
  //HtmlService.setTitle("This is MYTITLE"); 
  var result = '<-- Riepilogo Ordine --> \n';
  if (e.parameter == 'undefined') {
    result += "ERRORE SCONOSCIUTO NELL'ACQUISTO! non verrà inviato l'ordine";
    return ContentService.createTextOutput(result); 
  }
  else {
    
    var ID = '1sl0P4auOdX8i67kZ9LA8dGXm59I_fc_tSOaPaOpL1Ek';    //  identificativo della cartella di foglio (Spreadsheet ID)
//    var sheet = SpreadsheetApp.openById(sheet_id).getActiveSheet();
//    var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
    var Cartella = SpreadsheetApp.openById(ID);    // Cartella identificata da ID
    var Foglio = Cartella.getSheets()[0];          // nella parentesi quadra il foglio ---> 0 = primo, 1 = secondo, ecc. 
    var Cella_R = Foglio.getRange(2,5);            // riga 2, colonna 5 -> cella E2
    var Inserite = Cella_R.getValue() + 1;         // numero di righe inserite (letto dal foglio) incrementato di 1
    Cella_R.setValue(Inserite);                    // scrive nel fogio il valore incrementato di «Inserite»
    //    var Riga = Foglio.getLastRow() + 1;   
    var Riga = Inserite + 2;                       // Riga in cui  scrivere i dati
    result += 'Codice Ordine: #' + Riga;            // calcola la riga su cui scrivere i dati
    var Cella_D = Foglio.getRange(Riga,1);         // D = Data --------------> colonna 1
    var Cella_O = Foglio.getRange(Riga,2);         // O = Orario ------------> colonna 2
    var Cella_T = Foglio.getRange(Riga,3);         // T = Temperatura -------> colonna 3
    var Cella_U = Foglio.getRange(Riga,4);         // U = Umidità relativa --> colonna 4
    var Cella_C = Foglio.getRange(Riga,5);         // C = Cliente ----------->
    var answer = "";
//    SpreadsheetApp.setActiveSheet(sheet);
//    var rowData = [];
    var Giorno_Ora = new Date();                                                  // legge data ed ora attuali     
    var Data   = Utilities.formatDate(Giorno_Ora, "Europe/Rome", 'dd/MM/yyyy');   // estrae la data da Data_Ora 
    var Orario = Utilities.formatDate(Giorno_Ora, "Europe/Rome", 'HH.mm.ss'  );   // estrae l'orario da Data_Ora

    Cella_D.setValue(Data);                                 //scrive la Data
    Cella_O.setValue(Orario);                               //scrive l'orario
    for (var parametro in e.parameter) {        
      var valore = Pulisci(e.parameter[parametro]);         //estrae il valore del paramentro senza virgolette 
      
      switch (parametro) {     // condizione case   
        case 'P':
          Cella_T.setValue(valore);   // Scrive il prodotto
          result += '\nProdotto: ' + valore + '';    // Scrive messaggio risposta
        break;
        case 'C':
          Cella_C.setValue(valore);       // Scrive il cliente
          result += '\nCliente: ' + valore + '';     // Scrive messaggio risposta
        break;
        case 'Q':
          Cella_U.setValue(valore);      // Scrive la quantità
          result += '\nQuantità: ' + valore + ' pezzi';   // Scrive messaggio risposta
         break;  
        default:  //in caso di errore:
          result += "\n\nParametri non validi!. L'ordine non verrà preso in considerazione";
      }
    } //se tutto va bene passa avanti con il successo
    result += '\n\nEseguito con successo! Qualcuno provvederà a prendere in considerazione il suo ordine.';     
  }
  return ContentService.createTextOutput(result);   // Restituisce il Riepilogo dell'ordine
}
function Pulisci( value ) {
  return value.replace(/^["']|['"]$/g, "");  // sostituisce cosa a cosa???
}
  </script>
  </head>
<body>
    <p>Sicuro di voler procedere con l'ordine?</p> <br>
    <input id="clickMe" type="button" value="clickme" onclick="doGet();" />
</body>
</html>

The problem is that when I press the button to call the function doGet(), this is the error and the function doesn't go on:

userCodeAppPanel:5 Uncaught TypeError: Cannot read property 'parameter' of undefined
    at doGet (userCodeAppPanel:5)
    at HTMLInputElement.onclick (userCodeAppPanel:1)

Can pls somebody help me?

2 Answers2

1

ContentService, SpreadsheetApp, and even doGet(e) are server side functions they run on a Google Server not on your browser. You need to restructure your entire code and utilize some intermediate javascript functions and possibly google.script.run you would be well served to do a little more research in some of the examples provided here at SO.

The idea of using doGet(e) as a clientside function is rather ridculuous because the whole idea is that it's supposed to be an endpoint on the server. It's ridiculous to consider it as a clientside function.

Cooper
  • 59,616
  • 6
  • 23
  • 54
  • i don't know how to search, and if i find something i don't know to use it –  Jun 09 '21 at 14:14
  • @TizFaver you didn't need to say that as it's clear from your question that you lack understanding. You need to read the documentation and work out some tutorials and examples from SO. It takes a while to figure out serverside versus clientside javascript. – Cooper Jun 09 '21 at 14:29
  • While it's possible to use javascript server side, it's not possible to run google apps script classes clientside. I say that with some trepidation because there may be some isolated situations that I'm not considering but generally it's true. – Cooper Jun 09 '21 at 14:35
0

To resolve the issue you are facing, you need to change your button HTML element to the below code -

<input id="clickMe" type="button" value="clickme" onclick="doGet(event);" />

Since you are using inline event handler, you need to pass the event as argument to onClick method. Please refer to this post in-case you have any queries.

jateen
  • 642
  • 3
  • 13
  • ok thanks this resolved the problem, but now it say: Uncaught ReferenceError: SpreadsheetApp is not defined at doGet (userCodeAppPanel:14) at HTMLInputElement.onclick (userCodeAppPanel:1) –  Jun 09 '21 at 13:36
  • That must be related to your custom compoenet SpreasdshetApp not imported correctly. – jateen Jun 09 '21 at 13:39
  • and how i import them? –  Jun 09 '21 at 13:40
  • i tried searching: https://stackoverflow.com/questions/27295840/referenceerror-spreadsheetapp-is-not-defined . The SpreadsheetApp is related with google sheet –  Jun 09 '21 at 13:46
  • @Cooper I know you are master but that last line of yours is kinda offending. That user might be a beginner and might not have sort out everything. Least you can do is be considerate. Thanks. – jateen Jun 09 '21 at 15:49
  • @jateen The appropriate thing to do is to flag it and let the moderator handle it. – Cooper Jun 09 '21 at 16:10