Home > database >  Google Apps Script in VSCode
Google Apps Script in VSCode

Time:12-28

I'm creating my Google Apps Script(GAS) app with VSCode and clasp.

Now, "scriptlets" causes problems in my "index.html"(the picture below).

errors occur in my code

Does anyone know how to fix it?

CodePudding user response:

This is just my guess from your question. From new Chart of your showing script in the image, if you are using Chart.js, under type: 'bar', the value of data.datasets is required to be an array including the objects. If my understanding is correct, how about the following modification?

As sample data, if datasets in Google Apps Script side is as follows,

const data = [
  { label: 'sample A', data: [1, 2, 3, 4, 5], backgroundColor: "rgba(255,0,0,1)" },
  { label: 'sample B', data: [5, 4, 3, 2, 1], backgroundColor: "rgba(0,255,0,1)" },
];

how about the following modification?

Google Apps Script side:

// Please replace your data.
const data = [
  { label: 'sample A', data: [1, 2, 3, 4, 5], backgroundColor: "rgba(255,0,0,1)" },
  { label: 'sample B', data: [5, 4, 3, 2, 1], backgroundColor: "rgba(0,255,0,1)" },
];
const htmlTemplate = HtmlService.createTemplateFromFile("index"); // Please replace your actual HTML filename.
htmlTemplate.datasets = JSON.stringify(data);
const htmlOutput = htmlTemplate.evaluate();

// Please use htmlOutput in your script.

HTML side:

From:

datasets: [<?!= datasets ?>]

To:

datasets: <?!= datasets ?>
  • If your datasets is like '{"label":"sample A","data":[1,2,3,4,5],"backgroundColor":"rgba(255,0,0,1)"},{"label":"sample B","data":[5,4,3,2,1],"backgroundColor":"rgba(0,255,0,1)"}', I think that you can modify as follows. In this case, I think that you can use your current datasets: [<?!= datasets ?>].

    const htmlTemplate = HtmlService.createTemplateFromFile("index"); // Please replace your actual HTML filename.
    htmlTemplate.datasets = '{"label":"sample A","data":[1,2,3,4,5],"backgroundColor":"rgba(255,0,0,1)"},{"label":"sample B","data":[5,4,3,2,1],"backgroundColor":"rgba(0,255,0,1)"}'; // Please replace your data.
    const htmlOutput = htmlTemplate.evaluate();
    
    // Please use htmlOutput in your script.
    

Note:

  • Unfortunately, I cannot know your value of datasets and your whole script from your question. So, when my guess of your question was not correct, can you provide your whole script for correctly replicating your current issue? By this, I would like to confirm it.

Reference:

  • Related