0

How can I send the function result to HTML Template From File, with below effort I am getting an object Object error message

Error in HTML Page is : [object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]

function doGet() {
 var ss = SpreadsheetApp.openById("IIIIIII");
    var sheet = ss.getSheetByName('Sheet3')
    var values = sheet.getDataRange().getValues();
    var msg = [];

    var MyDate =  e.parameter.time;// 7/27/2020
    var d = new Date(MyDate);
    var n = d.getTime();
    for (var i = 0; i < values.length; i++) {
        if (new Date(values[i][0]).getTime() === n) {
            msg.push(values[i][1])
        }
    }

const ts = ['10:00-11:00', '11:00-12:00', '12:00-13:00', '13:00-14:00', '14:00-15:00', '15:00-16:00', '16:00-17:00', '17:00-18:00', '18:00-19:00'],
        booked3 = msg,
        avail = (ts, booked) =>
        ts.map(item => {
            const [start, end] = item.split('-'),
                isBooked = !booked
                .map(item => item.split('-'))
                .every(([bookedStart, bookedEnd]) => bookedStart >= end || bookedEnd <= start)
            return {
                slot: `${start}-${end}`,
                isBooked
            }
        })
console.log(avail(ts, booked3))
      
var template
var template =HtmlService.createTemplateFromFile('index'); 
     template.name= e.parameter.time;
     template.idata=avail(ts,booked3); // this result is showing in html page 
     var pageData= template.evaluate()
    .setTitle('System') 
    .setSandboxMode(HtmlService.SandboxMode.IFRAME) 
    .addMetaTag('viewport', 'width=device-width, initial-scale=1')
    .setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL) 
     return pageData;
}
<div class="text-white display-6">idata:<?= idata; ?></div>

Google Sheet

TheMaster
  • 45,448
  • 6
  • 62
  • 85
KiKu
  • 377
  • 5
  • 22

1 Answers1

1

Your code is passing an array of objects to the template HTML.

Replace

template.idata=avail(ts,booked3); 

by

template.idata=JSON.stringify(avail(ts,booked3)); 

If you want to significantly improve the presentation then your code should convert the result of avail(ts,booked3) to HTML.

Rubén
  • 34,714
  • 9
  • 70
  • 166