如何从 Google 应用程序脚本中的 HTML 文本获取变量值?

How to get value of variable from HTML text in Google Applicatioin Script?

我使用 UrlFetchApp.fetch(url,options) 获得了有效的 HTML 代码。我感兴趣的部分看起来像

<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>

您能否建议如何获取分配给 GAS 变量的 polygonSeries.data javascript 变量的值?除了逐行解析 HTML,找到 polygonSeries.data 然后解析直到得到 }]; 之外,我想不出任何其他方法,但我认为这不是最好的方法。

建议

您可以使用此示例正则表达式方法脚本并根据您的需要进行调整:

脚本:

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);
}

示例 Index.html 文件:

<!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:support@yourcompany.com">
support@yourcompany.com</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>

示例结果:

我根据 Irvin 的代码实现了这个。我最初是在寻找简单的解决方案,我不必使用任何类型的循环 - foreach 左右。

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])

}