2017-09-20 1 views
0

Ich versuche ein Array aus einer Tabelle zu bekommen und benutze es um ein Dropdown zu füllen. Hier ist mein Codegoogle.script.run gibt undefined zurück. Wie repariere ich es?

code.gs

function doGet() { 

    var page = HtmlService.createTemplateFromFile('Index'); 

return page.evaluate() 

} 

function include(filename) { 
    return HtmlService.createHtmlOutputFromFile(filename) 
     .getContent(); 
} 

function getArrayList(){ 

    var ss = SpreadsheetApp.openById("1EjRFAYiMBw5BuNhFJRUVG4MpUjIIy8Oey5JLyiYxj3c"); 
    var sheet = ss.getSheetByName("Nomes"); 
    var values = sheet.getDataRange().getValues(); 

    var array = []; 

    for each (var item in values){ 
    array.push(item[0]); 
    } 

    return array 

} 

Index.HTML

<!DOCTYPE html> 
<html> 

<?!= include('jQuery'); ?> 

    <div class="demo" > 

     <form id="myForm"> 
     <select id="selectNumber"> 
      <option>Choose a number</option> 
     </select> 
     </form> 

<?!= include('JavaScript'); ?> 

    </div> 
</html> 

JavaScript.html

<script> 
     var options = google.script.run.getArrayList(); 
     console.log(options); 

     $('#selectNumber').empty(); 
     $.each(options, function(i, p) { 
      $('#selectNumber').append($('<option></option>').val(p).html(p)); 
     }); 

</script> 

jQuery.html

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> 
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.9.1/jquery-ui.min.js"></script> 

Wenn statt var options = google.script.run.getArrayList(); auf der JavaScript.html Datei, passiere ich eine wörtliche Array var options = ["1","2","3"]; mein Code funktioniert wie ein Charme.

Ich weiß, dass das Problem ist, dass google.script.runUndefined zurückgibt, aber ich weiß nicht, was ich falsch mache. Kann mir bitte jemand helfen?

Antwort

1

Ist das nützlich?

google.script.run.function() gibt keine Werte zurück. So ist options von var options = google.script.run.getArrayList();Undefined. Verwenden Sie withSuccessHandler(), um die zurückgegebenen Werte von getArrayList() abzurufen. Unten ist die aktualisierte JavaScript.html.

<script> 
     google.script.run.withSuccessHandler(sample).getArrayList(); 

     function sample(options) { 
      $('#selectNumber').empty(); 
      $.each(options, function(i, p) { 
       $('#selectNumber').append($('<option></option>').val(p).html(p)); 
      }); 
     } 
</script> 
0

Werte ist ein Array von Arrays. Um Sie alle Daten müssen so etwas wie:

var a=[]; 
for(var i=0;i<values.length;i++) 
{ 
    for(var j=0;j<values[0].length;j++) 
    { 
    a.push(values[i][j]); 
} 
} 

oder nur Werte zurück