【问题标题】:How to get value of variable from HTML text in Google Applicatioin Script?如何从 Google 应用程序脚本中的 HTML 文本中获取变量的值?
【发布时间】:2021-11-16 03:31:33
【问题描述】:

我使用 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,然后解析直到我得到}];,我想不出任何办法,但我认为这不是最好的方法。

【问题讨论】:

  • 您可以在How can I convert HTML code into a JSON object 尝试类似帖子的答案,或者尝试使用regex 类似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/) 的东西,然后清理并将数据放入数组或对象中。
  • 我看不出stackoverflow.com/questions/60468365/… 会怎样。但是我使用了你的代码,在 6 行代码中我完成了我需要的。您要创建一个答案以便我接受吗?为什么我们需要去除空格?
  • 当然,我将发布正则表达式方法作为答案。我很高兴快速正则表达式方法对您有用。我正在删除 html 代码中的所有空格,因为在我使用正则表达式进行测试时,当有多个空格时它不起作用。

标签: javascript google-apps-script html-parsing


【解决方案1】:

建议

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

脚本:

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>

样本结果:

【讨论】:

  • 我使用了 val() 而不是 forEach,否则你的帮助非常棒。
  • 我明白了。不客气@Radek。如果我们回答了您的问题,请单击左侧的接受按钮(复选图标)。通过这样做,社区中可能与您有同样担忧的其他人会知道他们的问题可以得到解决,并且他们会在搜索时轻松看到此帖子。
【解决方案2】:

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

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-25
    • 2021-12-26
    • 2013-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-27
    • 2023-01-07
    相关资源
    最近更新 更多