search

Home  >  Q&A  >  body text

google.script.run.function() returns null

<p>On a Google Sheet, I have a sidebar form that allows the user to add information in another sheet. I have to provide an ID for each row so that when the user adds an ID, a new ID is generated. </p> <p>Example: The sidebar form has two inputs: standard number (int) and standard (text). When the user adds these inputs, I want to put them into a worksheet with 3 columns: A = ID, B = Standard Number, and C = Standard. To generate the IDs, I created a creeID() function on the server side (this function checks the last ID in the sheet and generates the next ID, worked well when testing) and when the user adds a row ("ajouter" button) to get a tab with this format <code>[ID, criteria number, criteria]</code> and push that tab using <code>appendRow()</code></code> to the worksheet p> <p>I thought I could use a google script to run with <code>.withSuccessHandler()</code> like it says here, I tried a few things based on @Tanaike's help and this video.</p> <p>一切似乎都工作正常,除了 <code>google.script.run.withSuccessHandler(data => {alert("preuve ajoutée")}).creeID()</code> 返回 <code>null</code ></p> <p>这是我的 html 文件:</p> <pre class="brush:php;toolbar:false;"><!DOCTYPE html> <html> <head> <base target="_top"> <style> ... </style> </head> <body> <p>Indicateur</p> <input type="number" name="indicateur" id="indic" value="" min = "1" max = "32"> <p>Preuve</p> <input type="text" name="preuve" id="preuve"> <br> <input type="button" value="Ajouter" onclick="ajouter()"> <span class="annuler" onclick="google.script.host.close()">Annuler</span> <script> function ajouter() { const inputs = document.querySelectorAll('#indic, #preuve'); let tab = [google.script.run.withSuccessHandler(data => {alert("preuve ajoutée")}).creeID()]; // Récupération des valeurs for (const input of inputs) { tab.push(input.value); } // Si tous les champs sont vides if (tab.join('') == '') { alert('Le formulaire est vide !'); return; } // Vider les champs inputs.forEach(input => input.value = ''); // Envoi dans la feuille google.script.run.ajouterLigne(tab); } </script> </body> </html></pre> <p>和 JavaScript 代码:</p> <pre class="brush:php;toolbar:false;">function ajouterLigne(tab) { const PREUVES = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("PREUVES"); console.log(tab) PREUVES.appendRow(tab); } function creeID() { const SHEET = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("PREUVES"); let lastRow = SHEET.getLastRow(); let lastIdRange = SHEET.getRange(lastRow, 1); let lastId = lastIdRange.getValue(); let newId; if (lastId == "ID") { newId = 1; } else { newId = lastId 1; }; return(newId) }</pre></p>
P粉366946380P粉366946380479 days ago581

reply all(1)I'll reply

  • P粉930448030

    P粉9304480302023-09-06 09:30:00

    For your script, google.script.run.creeID() does not return any value. I think this is the cause of your current problem. From Maybe I should use .withSuccessHandler() like it says here, but I don't know how. , how about using withSuccessHandler() as follows?

    In this case, modify Javascript's ajouter() as follows.

    Modified script:

    function ajouter() {
      const inputs = document.querySelectorAll('input[type="text"]');
      google.script.run.withSuccessHandler(tab => {
        tab = [tab];
        for (const input of inputs) {
          tab.push(input.value);
        }
        if (tab.join('') == '') {
          alert('Le formulaire est vide !');
          return;
        }
        inputs.forEach(input => input.value = '');
        google.script.run.ajouterLigne(tab);
      }).creeID();
    }
    
    • In this modification, when the button is clicked, the value of tab is retrieved and the value of tab and the entered value are appended to the spreadsheet.

    refer to:

    reply
    0
  • Cancelreply