Return spreadsheet values to javascript
<p>I'm quite annoyed by a problem with google script and javascript.
I have a problem to send datarange values from my google script to be treated by my javascript function.</p>

<p>Here is my code.</p>

<p>code.gs extract:</p>

<pre><code>function getSheetData(ss,sh){
    // Create sheet object
    var ass = SpreadsheetApp.openById(ss);
    SpreadsheetApp.setActiveSpreadsheet(ass);
    var ash = SpreadsheetApp.openById(ss).getSheetByName(sh);
    ash = SpreadsheetApp.getActive();
    var result = ash.getDataRange().getValues();
    Logger.log("getSheetData(ss,sh) result : "+result);
    // return result after JSON strinfigy
    return JSON.stringify(result);
}
</code></pre>

<p>JavaScript.html extract :</p>

<pre><code>function readTb(fn) {
    var result = google.script.run.getSheetData(bdData, tbData);
    console.dir('readTb result : ' + result);
    fn(result);
}

function buildSelect(range) {
    console.log('Range = ');
    console.dir(range);
    if (range.length &gt; 0) {
        buildOption('', 'Choose an order');
        for (i = 0; i &lt; range.length; i++) {
            var row = range[i];
            buildOption(row[0], row[0] + " ~ " + row[2] + " ~ " + row[5]
                    + " ~ " + row[7]);
        }
    } else {
        buildOption('', 'No order to display');
    }
}

function buildOption(data) {
    console.log(data);
}

/**
 /* Retrieve orders and format to fill select numCommande input
 /*
 */

function listCommandesPesee() {
    console.log('Call for orders seeking');
    // Read DB and retrieve data
    // feed function variables with CONST
    bdData = BDDATA_OLD;
    tbData = TBCOMMANDES;
    // Call readTb to retrieve data, then buildSelect to format result
    var promise1 = new Promise(readTb);
    promise1.then(buildSelect);
    console.dir(promise1);
}
</code></pre>

<p>By now, Logger.log in GS IDE shows right data:</p>

<pre>
[16-07-27 08:01:22:040 PDT] résultat de getSheetData(ss,sh) : Ligne,ID,Produit,Date,Fournisseur,Numéro Camion,Silo,Cellule,Ilot,Poids coop,Poids net livré,N° analyse qualité,Cellule destination ,Transport
...
</pre>

<p>on page load, listCommandesPesee is called, which call readTb().</p>

<p>But even with json stringify, I still have an undefined value in var result (readTb function) and so range in buildSelect doesn't have any length property.</p>

<p>JSON.Stringify was working when I was testing and before I add promise and chain (I do think)</p>

<p>Because I'm stuck on this. I'll be grateful for any help.</p>

<p>Thanks</p>