Google apps script UI services to HTML services

前端 未结 1 1189
心在旅途
心在旅途 2021-01-07 08:32

I try to convert this simple google apps script code below to HTML services code. The code below is written with the deprecated google apps script UI servi

相关标签:
1条回答
  • 2021-01-07 08:52

    Your Ui output looks like this:

    Ui Output

    Create an HTML file, and enter this code:

    testForm.html

    <div>
      <div>
          Name: <input id='idNameField' type='text'/>
      </div>
    
      <br/>
    
      <input type='button' value='Verstuur' onmouseup='runGoogleScript()'/>
    </div>
    
    <script>
      function onSuccess(argReturnValue) {
        alert('was successful ' + argReturnValue);
        //Reset fields on screen
        Document.getElementById('idNameField').value = "";
      }
    
      function runGoogleScript() {
        console.log('runGoogleScript ran!');
    
        var inputValue = document.getElementById('idNameField').value;
    
        google.script.run.withSuccessHandler(onSuccess)
          .InsertInSS(inputValue);
      };
    
    </script>
    

    Copy the follow code into:

    Code.gs

    function doGet() {
    
      return HtmlService.createTemplateFromFile('testForm')
        .evaluate() // evaluate MUST come before setting the NATIVE mode
        .setTitle('The Name of Your Page')
        .setSandboxMode(HtmlService.SandboxMode.NATIVE);
    };
    

    In a seperate .gs file add this code:

    function InsertInSS(argPassedInName){
      var ssKey = 'sheetkey....';
    
      var SS = SpreadsheetApp.openById(ssKey);
      var Sheet = SS.getSheetByName('Contacts');
      Sheet.getRange(Sheet.getLastRow()+1, 1, 1, argPassedInName.length).setValue(argPassedInName);
    }
    
    0 讨论(0)
提交回复
热议问题