简体   繁体   中英

How can I call a function from another function and how?

This is my JavaScript written in HTML file itself where I want to call a value(Time) from a function written in code.gs file for getting time from spreadsheet. Also the function written below in JavaScript will run onclick of the button with the new URL having parameters of time, the value called from spreadsheet code.gs

<script >
    const getTimefromSheet = () => new Promise((resolve, reject) => google.script.run.withSuccessHandler(resolve).withFailureHandler(reject).getTimefromSheet());
    async function gettime(){
      const getTimefromSheett = await getTimefromSheet().catch(err => console.log(err));
      //  getTimefromSheett = google.script.run.getTimefromSheet();
     console(getTimefromSheett);
     return getTimefromSheett;
    }
     document.getElementById('submit').addEventListener('click', _ => {
      var getT = gettime();
      addRow();
      console.log(getT);
      const css = document.getElementById("txt").value;
      document.getElementById('submit').href = <?= ScriptApp.getService().getUrl() ?> + `?v=form&input=${getT}`;

    }); 
  // google.script.url.getLocation(location);

The fuction which will return time is as follows: code.gs

function getTimefromSheet(){
  var sheetname = "Input";
  var rang = "C2";
  var tr = getDataFromSheet(sheetname,rang);
  
  var timestamp = tr.toString().replace(/\s/g, "");
  Logger.log(timestamp);
  return timestamp;
}

Kindly let me know that how can one pass the value from spreadsheet to the URL in parameters. I would need the unique URL for every user Hence this is the requirement for the same. I am new to this so how can I achieve?

As a simple modification, how about the following modification?

Modified script:

In this modification, Javascript is modified.

const getTimefromSheet = () => new Promise((resolve, reject) => google.script.run.withSuccessHandler(resolve).withFailureHandler(reject).getTimefromSheet());

document.getElementById('submit').addEventListener('click', async _ => {
  var getT = await getTimefromSheet().catch(err => console.log(err));
  addRow();
  console.log(getT);
  const css = document.getElementById("txt").value;
  document.getElementById('submit').href = <?= ScriptApp.getService().getUrl() ?> + `?v=form&input=${getT}`;
});
  • When above script is run, getT is the value from getTimefromSheet() .
  • In this modification, it supposes that getTimefromSheet() and addRow() works fine, and also, <?= ScriptApp.getService().getUrl()?> works fine. Please be careful this.

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