简体   繁体   中英

Get spreadsheet ID through HTML

I'm trying to get the customer to enter the ID of their spreadsheet, so that the remaining script performs the proper functions on the customer's spreadsheet. My code is currently like this:

Script.gs

var id;
var spreadsheet=SpreadsheetApp.openById(id);
function myfunction(x) {
    id=x;
}

function doGet() {
  var template = HtmlService.createTemplateFromFile('HTML')
  var html=template.evaluate();
  return html
}

HTML:

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    <input>ID></input>
    <button onclick="passId()">PASS</button>
    <script>

     function passId() {
      google.script.run.myfunction(id);
     }   

    </script>
  </body>
</html>

When I try to view the HTML, it returns an error saying that the value of the ID variable is invalid. The intention is for the client to inform the ID of his worksheet, as the rest of the code depends on the ID of the worksheet to work. If I assign the id manually through.gs, the HTML works perfectly in the worksheet with the id assigned, thus applying all the functions present in my code.

EDIT

I made some changes according to the answers, and it looks like this

GS

function logUserInput(x){
  var id=x;
  Logger.log(id);
  return id;
}
function doGet(e) {
  var template = HtmlService.createTemplateFromFile('HTML');
  template.variablename = "";
  var html=template.evaluate();
  return html;
}

HTML

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    <input id="KK" type="text" value="<?= variablename ?>"/>
    <button onclick="passId()">PASS</button>
    <script>

     function passId() {
     google.script.run.logUserInput(document.getElementById("KK").value);
     } 

    </script>
  </body>
</html>

Now I will try to assign the value that is inside the "logUserInput(x)" function to a global variable. Thanks!

In Google Apps Script, stuff set out of any function, the global scope, is executed every time that a server side function, including doGet, is ran from the editor, a user interface, including a web application, or by simple/installable triggers.

So first thing that you should do is to remove the following lines from the global scope.

var id;
var spreadsheet=SpreadsheetApp.openById(id);

Below is a simplified implementation of a web app that pass a value entered in a web application created using Google Apps Script to the server side code. Please checkout the comments.

You might try this code or adapt it to your current code.

/**
 * Creates the web application using an .html file
 */
function doGet(e){
  return HtmlService.createHtmlOutputFromFile('index');
}
/**
 * Function called from the client-side. It logs the value 
 * entered by the web-app user.
 */
function logUserInput(id){
  console.log(id);
  return `${id} successfully logged`;
}

index.html Contains the web application structure and client-side JavaScript

<!DOCTYPE html>
<html>
  <head>
  </head>
  <body>
    <label for="my-input">Enter the spreadsheet id</label><br>
    <input id="my-input" type="text"><br>
    <button onclick="sendToServer">Save</button>
    <script>
      /**
       * Called by the button click event.
       * Sends the input value to the server side and logs the server side
       * returned string or the error message if it occurs on the server side
       */
      function sendToServer(){
        /** Retrieves the input value */
        const id = document.querySelector('input').value;
        /** Send the input value to the server */
        google.script.run
        .withSuccessHandler(console.log)
        .withFailureHandler(console.error)
        .logUserInput(id);
      }
    </script>
  </body>
</html>

References

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM