0

I wrote a script for my Google Spreadsheet and but it doesn't work as expected. I have a Sheet with customer data. The first column (column A) shouldn't get an entry because that's the customer ID and it will be automatically set when I put a name in column B. So I want the script to start the entry from the second column (column B) on.

How do I tell my script to start the entry from the second column (column B)? It currently starts the entry from column A.

My script

function onOpen(){
var ui=SpreadsheetApp.getUi();
ui.createMenu("Eintrag")
  .addItem("Sidebar form", "showInsidebarform")
  .addToUi();
}
//OPEN THE FORM IN SIDEBAR
function showInsidebarform() {
  var userform=HtmlService.createTemplateFromFile("form").evaluate().setTitle("Daten eintragen");
   SpreadsheetApp.getUi().showSidebar(userform);
}

function appenData(data){
  var ws=SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Schüler")
    ws.appendRow([data.name,data.vname]);
}

The html code

 <!DOCTYPE html>
  <html>
    <head>
      <!--Import Google Icon Font-->
      <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
       <!-- Compiled and minified CSS -->
      <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">

      <!--Let browser know website is optimized for mobile-->
      <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    </head>

    <body>
    </div class="container">
    <div class="row">
        <div class="input-field col s12">
          <i class="material-icons prefix">account_circle</i>
          <input id="name" type="text" class="validate">
          <label for="name">Nachname</label>
      </div>
        <div class="input-field col s12">
          <i class="material-icons prefix">account_circle</i>
          <input id="vname" type="text" class="validate">
          <label for="vname">Vorname</label>
      </div>
        <div class="input-field col s12">
        <button class="btn waves-effect waves-light" id="btn">Submit
        <i class="material-icons right">send</i>
        </button>
      </div>

    </div><!--end row-->

    </div>
      <!-- Compiled and minified JavaScript -->
     <script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>
     <script>
      var nameBox=document.getElementById("name");
      var vnameBox=document.getElementById("vname");
      document.getElementById("btn").addEventListener("click",addRecord);
        function addRecord(){
         var name=nameBox.value;
         var vname=nameBox.value;
          if(name.trim().length==0||vname.trim().length==0){
          M.toast({html: 'haha'})
          } 

        var data={
        name:nameBox.value,
        vname:nameBox.value    
      };

      google.script.run.appenData(data);
      nameBox.value="";
      vnameBox.value="";
      }
     </script>
    </body>
  </html>
        
TheMaster
  • 45,448
  • 6
  • 62
  • 85
neutral
  • 1
  • 3
  • Get the appropriate range and use `setValues()` instead of `appendRow()` – TheMaster Nov 10 '21 at 14:41
  • Could you eloberate that? I'm relatively new to the stuff. How does the script has to look like when I use setValues? I appreciate your help – neutral Nov 10 '21 at 14:53
  • You said you wrote this script. Didn't you read the documentation? Take a look [here](https://stackoverflow.com/questions/11334296/how-to-get-the-correct-range-to-set-the-value-to-a-cell). Also See [tag info page](https://stackoverflow.com/tags/google-apps-script/info) for free resources and more details. – TheMaster Nov 10 '21 at 14:54
  • Learn how to build an html form and use window.onload to delay you javascript until window has loaded. Users without prior experience with javascript, html, css development may find html construction difficult. – Cooper Nov 10 '21 at 15:00

0 Answers0