How to put values in html into variables with code .gs

Asked 1 years ago, Updated 1 years ago, 68 views

I created a select box in index.html that I created in Google Apps Script.
When I select a county name, I want to put that value in a variable in code .gs, but I don't know how to get it and how to use it in code .gs.

index.html

<form name="kenform">
    <select name="ken" onChange="kakunin()">
        <option value="Country name 1">Country name 1</option>
        <option value="Country name 2">Country name 2</option>
        <option value="Country name 3">Country name 3</option>
    </select>
</form>

What should I do if I want to use the value (prefectural name) obtained here instead of the "SHEET_NAME" variable on the GAS side?

Code .gs

var SPREAD_ID='xxxxxxxxxxxx';
varSHEET_NAME= 
ss=SpreadsheetApp.openById(SPREAD_ID);
sheet=ss.getSheetByName(SHEET_NAME); 

I would appreciate it if you could give me some advice.

javascript html google-apps-script

2022-09-30 21:17

1 Answers

Google.script.run should work.

HTML Service:Communicate with Server Functions\|Apps Script\|Google Developers

index.html

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    <form name="kenform">
      <select name="ken" onChange="google.script.run.kakunin(this.value)">
        <option value="Country name 1">Country name 1</option>
        <option value="Country name 2">Country name 2</option>
        <option value="Country name 3">Country name 3</option>
       </select>
    </form>
  </body>
</html>

index.gs

function doGet(e){
  return HtmlService.createTemplateFromFile('index').evaluate();
}

function kakunin(e){
  Logger.log(e);
}

Log

 [16-07-29 18:20:43:432JST] State Name 2


2022-09-30 21:17

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.