简体   繁体   中英

HtmlService: google.script.run not recognizing gs function

I'm currently trying to pass an array of values from a Google Sheet to the HtmlService where I will have the user choose an option and eventually pass it back to the .gs script. I have been using these two links as references: 1. Google Documentation 2. Stack Overflow example

When running the code, I looked at my console and noticed this error: VM3051:4 Uncaught TypeError: google.script.run.withSuccessHandler(...).getVersionArray is not a function It appears that getVersionArray() is not being passed correctly. When removing this function from the rest of that google.script.run call, the error goes away.

Also, per link two, I tried that code with the template and never even got a window to pop up, so I have been using the HtmlOutput example from the Google documentation link as a starting point. I have also tried the code with and without the SandboxMode declaration.

gs code:

function bugPieChart() {
  getVersionArray();
  openDialog();

function getVersionArray() {

  var ss = SpreadsheetApp.getActive();
  var valuesR = ss.getSheetByName("report").getRange('R1:R').getValues();
  var valuesS = ss.getSheetByName("report").getRange('S1:S').getValues();

  var versionRSArray = [];

  for (var i = 0; i < valuesR.length; i++) {
    versionRSArray.push(valuesR[i][0]);
  }

  for (var i = 0; i < valuesS.length; i++) {
    versionRSArray.push(valuesS[i][0]);
  }

  versionRSArray.sort();

  var uniqueArray = [];

  uniqueArray.push(versionRSArray[0]);

  for (var i in versionRSArray ) {  
    if((uniqueArray[uniqueArray.length-1]!=versionRSArray[i]) && (versionRSArray[i] !== "")) {
      uniqueArray.push(versionRSArray[i]);
    }
  }
  return uniqueArray;
}

function openDialog() {    
  var html = HtmlService.createHtmlOutputFromFile('index');
  SpreadsheetApp.getUi().showModalDialog(html, 'Dialog title');
  var htmlOutput = html.setSandboxMode(HtmlService.SandboxMode.NATIVE);
  return htmlOutput;  
}

}

index.html:

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
    <script>
      $(function() {
        google.script.run.withSuccessHandler(buildOptionsList)
            .getVersionArray();
      });
      function buildOptionsList(uniqueArray) {
        var list = $('#optionList');
        list.empty();
        for (var i = 0; i < uniqueArray.length; i++) {
          list.append('<option value="' + uniqueArray[i].toLowerCase() + '">' + uniqueArray[i] + '</option>');
        }  
      }      
    </script>
  </head>
  <body>
    <select id="optionList">
      <option>Loading...</option>    
    </select> 
    <input type="button" value="Close" onclick="google.script.host.close()" />
  </body>
</html>

I think your just missing a closing bracket on the function above it.

function bugPieChart() {
  getVersionArray();
  openDialog();
}

function getVersionArray() {

  var ss = SpreadsheetApp.getActive();
  var valuesR = ss.getSheetByName("report").getRange('R1:R').getValues();
  var valuesS = ss.getSheetByName("report").getRange('S1:S').getValues();

  var versionRSArray = [];

  for (var i = 0; i < valuesR.length; i++) {
    versionRSArray.push(valuesR[i][0]);
  }

  for (var i = 0; i < valuesS.length; i++) {
    versionRSArray.push(valuesS[i][0]);
  }

  versionRSArray.sort();

  var uniqueArray = [];

  uniqueArray.push(versionRSArray[0]);

  for (var i in versionRSArray ) {  
    if((uniqueArray[uniqueArray.length-1]!=versionRSArray[i]) && (versionRSArray[i] !== "")) {
      uniqueArray.push(versionRSArray[i]);
    }
  }
  return uniqueArray;
}

function openDialog() {    
  var html = HtmlService.createHtmlOutputFromFile('index');
  SpreadsheetApp.getUi().showModalDialog(html, 'Dialog title');
  var htmlOutput = html.setSandboxMode(HtmlService.SandboxMode.NATIVE);
  return htmlOutput;  
}

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