0

I get valid HTML code using UrlFetchApp.fetch(url,options). The part I am interested in looks like

<script type="text/javascript">

    var map = am4core.create("mapdiv", am4maps.MapChart);
    map.geodata = am4geodata_worldLow;
    map.projection = new am4maps.projections.Miller();
    var polygonSeries = new am4maps.MapPolygonSeries();
    polygonSeries.useGeodata = true;
    map.series.push(polygonSeries);
    // Configure series
    var polygonTemplate = polygonSeries.mapPolygons.template;

    polygonSeries.data = [{
                  "id": "AF",
                      "value0": "2",
                  "value3": 3.2,
                  "fill": am4core.color("#0C6175")
            }, {
                  "id": "AL",
                      "value0": "2",
                  "value3": 2.5,
                  "fill": am4core.color("#0C6175")
            }, {
                  "id": "DZ",
                  "name": "Algeria",
                      "value0": "1",
                  "value3": 3.2,
                  "fill": am4core.color("#68C2C3")
            }];
    polygonTemplate.propertyFields.fill = "fill";


</script>

Could you suggest how to get the value of polygonSeries.data javascript variable assigned to GAS variable? I cannot think of anything besides parsing the HTML line by line, find polygonSeries.data and then parse till I get }]; I do not think it is the best way though.

3
  • 1
    You can try the answer from this similar post at How can I convert HTML code into a JSON object Or alternatively try using regex something like var clean = htmlcontent.replace(/ /g,''); var regExp = new RegExp("polygonSeries.data=(.*)polygonTemplate", "s"); var data = regExp.exec(clean)[1]; var arr = data.split(/\r?\n/) then clean & place data into an array or object. Commented Sep 22, 2021 at 20:46
  • I cannot see how stackoverflow.com/questions/60468365/… would he me. But I used your code and in 6 lines of code I accomplished what I needed. Do you want create an answer so I can accept it? Why do we need to get rid of spaces? Commented Sep 23, 2021 at 9:41
  • Sure, I'll post the regex method as answer instead. I"m glad that the quick regex method worked on your end. I'm getting rid of all the spaces in the html code because on my testing with regex, it doesn't work when there are multiple spaces. Commented Sep 23, 2021 at 15:48

2 Answers 2

1

Suggestion

You can use this sample regex method script & adjust it based on your needs:

Script:

function getData() {
  var htmlcontent = HtmlService.createHtmlOutputFromFile('Index').getContent(); //Sample line to get the content of the Html file
  var clean = htmlcontent.replace(/ /g,''); //Clean the code by removing multiple spaces
  var regExp = new RegExp("polygonSeries.data=(.*)polygonTemplate", "s");
  var data = regExp.exec(clean)[1];
  var arr = data.split(/\r?\n/) //split all data by new lines
  var newArr = []; //container of all the values
  arr.forEach(res => { //Sample lines of code to clean each values to be placed later as array values
    if(res.length > 3){
      try{
      var temp = res.replace(",","").split(":");
      newArr.push([temp[0].replace(/"/gm, ''),temp[1].replace(/"/gm, '')])
      }catch{
        var temp = res.split(":");
        newArr.push([temp[0].replace(/"/gm, ''),temp[1].replace(/"/gm, '')])
      }
    }
  });
  Logger.log(newArr);
}

Sample Index.html file:

<!DOCTYPE html>
<HTML>
<HEAD>
<TITLE>Your Title Here</TITLE>
    <script type="text/javascript">

        var map = am4core.create("mapdiv", am4maps.MapChart);
        map.geodata = am4geodata_worldLow;
        map.projection = new am4maps.projections.Miller();
        var polygonSeries = new am4maps.MapPolygonSeries();
        polygonSeries.useGeodata = true;
        map.series.push(polygonSeries);
        // Configure series
        var polygonTemplate = polygonSeries.mapPolygons.template;

        polygonSeries.data = [{
                      "id": "AF",
                      "value0": "2",
                      "value3": 3.2,
                      "fill": am4core.color("#0C6175")
                }, {
                      "id": "AL",
                      "value0": "2",
                      "value3": 2.5,
                      "fill": am4core.color("#0C6175")
                }, {
                      "id": "DZ",
                      "name": "Algeria",
                      "value0": "1",
                      "value3": 3.2,
                      "fill": am4core.color("#68C2C3")
                }];
        polygonTemplate.propertyFields.fill = "fill";


    </script>
</HEAD>
<BODY BGCOLOR="FFFFFF">
<CENTER><IMG SRC="clouds.jpg" ALIGN="BOTTOM"> </CENTER>
<HR>
<a href="http://somegreatsite.com">Link Name</a>
is a link to another nifty site
<H1>This is a Header</H1>
<H2>This is a Medium Header</H2>
Send me mail at <a href="mailto:[email protected]">
[email protected]</a>.
<P> This is a new paragraph!
<P> <B>This is a new paragraph!</B>
<BR> <B><I>This is a new sentence without a paragraph break, in bold italics.</I></B>
<HR>
</BODY>
</HTML>

Sample Result:

enter image description here

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

2 Comments

instead of forEach I used val(), otherwise your help was just great.
I see. You're welcome @Radek. If we answered your question, please click the accept button on the left (check icon). By doing so, other people in the community, who may have the same concern as you, will know that theirs can be resolved & they'll easily see this post on their searches.
1

Based on Irvin's code I implemented this one. I was originally looking for simple solution where I would not have to use any kind of cycle - for nor each or so.

function getData(){

  var html = getZZStat()
  var clean = html.replace(/=/g,'') // remove = otherwise eval() would not work   
  var endString = "}]"
  var regExp = new RegExp("polygonSeries.data(.*)"+endString, "s");   
  var data = regExp.exec(clean)[1]+endString


  var tmp = data.replace(/\)/g,'').replace(/am4core.color\(/g,'') // remove variable "am4core" so eval() works
  var finalData = eval(tmp)

  console.log("finalData ",finalData.length)
  console.log(finalData[0])
  console.log(finalData[finalData.length-1])
  console.log(finalData[finalData.length-2])

}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.