0

I'm struggling to append sheets names to a Bootstrap Table. My Apps Script Sidebar is called with sidebarSM() function:

function sidebarSM() {
  let html = HtmlService.createTemplateFromFile('public/sheets');
  html.wsNames = getWorksheetNames();
  html = html.evaluate(); ui().showSidebar(html)
}

The code.gs function:

function getWorksheetNames() {
  let sheetNames = new Array();
  let sheets = SpreadsheetApp.getActive().getSheets();
  for (let i = 0 ; i < sheets.length ; i++) sheetNames.push( { name: sheets[i].getName() } )
  return sheetNames;
}

And frontend html:

<script>

WorksheetDeleteApp.loadWorksheetNames = function() {
  let wsNames = JSON.parse("<?=JSON.stringify(wsNames)?>");
  let $table = $('#table-sm');
  $table.bootstrapTable('append', wsNames);
}

$(document).ready(function() {
  WorksheetDeleteApp.loadWorksheetNames()
});

</script>

I tried to pass the return with google.script.run in the html and it append the names, but it takes many seconds so I'm working with this JSON.parse("<?=JSON.stringify(wsNames)?>"); scripplet. wsNames variable shows correctly the names in a window.alert(), but dont append the names to the table. Any help?

Thanks.

1 Answer 1

1

In public/sheets.html, use:

  $table.bootstrapTable('append', <? wsNames ?>);

Alternatively, delete the html.wsNames = getWorksheetNames() line and call the function directly from public/sheets.html:

    <? const wsNames = getWorksheetNames(); ?>

See Pushing variables to templates.

Sign up to request clarification or add additional context in comments.

2 Comments

I tried your solutions but I'm not getting a solution :( I used that scriptlet inside script tags because I saw this answer stackoverflow.com/a/51697158/19617928 and it worked for me passing a boolean variable. It also read the JSON return of getWorksheetNames() in a window.alert(), but it does not append anything to the table
You do not need to use JSON. Just use a printing scriptlet. See the Web App Demo for simple sample code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.