user4411473 user4411473 - 4 months ago 27
Javascript Question

return every value from for loop?

I'm trying to return every value from the array

allArtistsArray
, which gets values from a spreadsheet, and have them display as an unordered list with
buttonTemplate
first, and then every value in the spreadsheet after it.

The problem I'm having is that only the first value from the spreadsheet is being returned and displayed on the web app. How do I get every value to display after
buttonTemplate
?

 

What's being displayed is:

* buttonTemplate

or

* value 1 from spreadsheet


 

What I'm trying to get displayed is:

* buttonTemplate
* value 1 from spreadsheet
* value 2 from spreadsheet
* value 3 from spreadsheet
* etc


 




 

index.html

<!DOCTYPE html>
<html>
<head>
<base target="_top">
<?!= getContent("js") ?>
<?!= getContent("css") ?>
<link rel="stylesheet" type="text/css" href="https://fonts.googleapis.com/css?family=Abel">
</head>
<body>
<div id="artistTabs">
<ul id="artistList">
<?!= createArtistList(); ?>
</ul>
</div>
</body>
</html>


 

code.gs

var ss = SpreadsheetApp.openById('id');
var sheet = ss.getSheets()[0];

function doGet()
{
return HtmlService.createTemplateFromFile('index').evaluate();
}

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

function createArtistList()
{
var buttonStartingRow = 2;
var buttonStartingColumn = 1;

var buttonCell = sheet.getRange(buttonStartingRow, buttonStartingColumn).getValue();

var x = '<li><a onClick="addArtist(); return false;" href="">';
var y = buttonCell;
var z = '</a></li>';

var buttonTemplate = x + y + z;

//return buttonTemplate;

var startingRow = 2;
var startingColumn = 1;
var howManyRows = sheet.getLastRow() - 1;
var howManyColumns = 1;

var allArtistsArray = sheet.getRange(startingRow, startingColumn, howManyRows, howManyColumns).getValues(); //get every name in 1st column after second row
//allArtistsArray = allArtistsArray.filter(function(n){return n[0] !== '' && n[0] !== buttonCell}); //filter 'buttonCell' value and blank rows
//allArtistsArray = allArtistsArray.toString().split(","); //flatten 2d array to 1d array

//Logger.log(allArtistsArray);

for (i = 0; i < allArtistsArray.length; i++)
{
allArtistsArray = allArtistsArray.filter(function(n){return n[0] !== '' && n[0] !== buttonCell}); //filter 'buttonCell' value and blank rows
allArtistsArray = allArtistsArray.toString().split(","); //flatten 2d array to 1d array

if (allArtistsArray == '')
{
Logger.log("array = blank");
break; //leave for loop and only return buttonTemplate ???
}
else
{
var x1 = '<li><a onClick="test(); return false;" href="">';
var z1 = '</a></li>';

var _1 = allArtistsArray[i];
var _2 = x1 + _1 + z1;

Logger.log(_2);
}
}
Logger.log(allArtistsArray);
return buttonTemplate;
}

Answer

Looks to me like you should be changing the value of buttonTemplate IN your for loop if that is the result you want. For example: buttonTemplate += _2 (or whatever you want appended it is unclear from your example).

EDIT

Without a snippet to play around with it's hard to see if this works but here is a better example of what I meant:

function createArtistList()
{
  var buttonStartingRow = 2;
  var buttonStartingColumn = 1;

  var buttonCell = sheet.getRange(buttonStartingRow, buttonStartingColumn).getValue();

  var x = '<li><a onClick="addArtist(); return false;" href="">';
  var y = buttonCell;
  var z = '</a></li>';

  var buttonTemplate = x + y + z;
  var artistsOutput = '';
  //return buttonTemplate;

  var startingRow = 2;
  var startingColumn = 1;
  var howManyRows = sheet.getLastRow() - 1;
  var howManyColumns = 1;

  var allArtistsArray = sheet.getRange(startingRow, startingColumn, howManyRows, howManyColumns).getValues(); //get every name in 1st column after second row
  //allArtistsArray = allArtistsArray.filter(function(n){return n[0] !== '' && n[0] !== buttonCell}); //filter 'buttonCell' value and blank rows
  //allArtistsArray = allArtistsArray.toString().split(","); //flatten 2d array to 1d array

  //Logger.log(allArtistsArray);

  for (i = 0; i < allArtistsArray.length; i++)
  {
    allArtistsArray = allArtistsArray.filter(function(n){return n[0] !== '' && n[0] !== buttonCell}); //filter 'buttonCell' value and blank rows
    allArtistsArray = allArtistsArray.toString().split(","); //flatten 2d array to 1d array

    if (allArtistsArray == '')
    {
      Logger.log("array = blank");
      break; //leave for loop and only return buttonTemplate ???
    }
    else
    {
      var x1 = '<li><a onClick="test(); return false;" href="">';
      var z1 = '</a></li>';

      var _1 = allArtistsArray[i];

      if (_1 != null)
      {
        var _2 = x1 + _1 + z1;

        artistsOutput += _2;
        Logger.log(_2);
      } else {
        Logger.log('The ' + i + 'th element was null for some reason');
      }
    }
  }
  Logger.log(allArtistsArray);
  return buttonTemplate + artistsOutput;
}