In google apps script, i've got an html file with an onsuccess callback function that calls a server side function, which grabs some data from the spreadsheet in question, then should return the data, stored in a variable, back to the html script function onsuccess.
i've already done this before successfully. this is why i am so perplexed at why, for some reason, the variable that i am returning from server side becomes 'undefined' once it is back to client side function.
i assumed it is some kind of Type Error, so i've played around with converting the variable (which is a very small array with three elements at most) and/or elements of the array to known types (string, int) and then pushing those into new variables and making a new array that contains the new known-type elements. also checked the original array elements using "typeof", and they are strings, as expected, so i became even more confused, because it doesn't seem like a TypeError.
html call-back function: (edited down to show essentials)
google.script.run.withSuccessHandler(
function (flavors)
{
console.log(flavors); // undefined
}
).getBatchFlavs();
code.gs: (edited down to show essentials)
function getBatchFlavs() {
var ss = SpreadsheetApp.getActive();
var sheet = ss.getSheetByName("Flavoring");
var data = sheet.getDataRange().getValues();
var flavCell = sheet.getRange(1,3);
data.forEach(function(row,i) {
var rule = flavCell.getDataValidation();
if (rule != null)
{
var flavs = rule.getCriteriaValues();
var flavors = flavs[0];
Logger.log(flavors); // shows exactly what i'm expecting
Logger.log(typeof flavors);
return flavors;
}
})
}
console.log prints "undefined".
I included the part of the .gs code where i am getting the data for the variable i want to pass to client-side from a cell with datavalidation options which are strings. i thought perhaps this may be creating some weird variable type or something unknown, but as i said, i already checked the type with "typeof" and the items in the flavors array are indeed strings.
There are some restrictions in what can be passed. Please read this Parameters & Return Values
I did this example and this works:
function showmydialog() {
var html='<html><head><script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"><script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script></head><body>';
html+='<div id="tst"></div>';
html+='<script>$(function(){google.script.run.withSuccessHandler(function(vA){$("#tst").html(vA.join());}).getMyFlavors();});</script>';
var ui=HtmlService.createHtmlOutput(html);
SpreadsheetApp.getUi().showModelessDialog(ui, "Flavors");
}
function getMyFlavors() {
var ss=SpreadsheetApp.getActive();
var sh=ss.getSheetByName('Sheet2');
var rg=sh.getRange('A1');//this has the data validation from the flavors named range
var rule = rg.getDataValidation();
if (rule != null)
{
var flavs = rule.getCriteriaValues();
//return flavs[0].getValues();
return rule.getCriteriaValues()[0].getValues().map(function(r){return r[0];}); //This should flatten out the array.
}
}
I created a named range called flavors.
This is what my spreadsheet looks like:
This is what my dialog looks like:
The data gets loaded onto the page via the dom ready function.
getBatchFlavs()
. The return flavors;
only returns the value to anonymous callback function passed to the Array#forEach
. return
something from the function called from google.script.run
- getBatchFlavs()
function getBatchFlavs() {//<================
data.forEach(function(row, i) {//<== |
if (rule != null) {// | |
var flavors = flavs[0];// | |
Logger.log(flavors); // | |
return flavors;//============== |
}// |
});// |
return flavors;//=========================
}
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.