【问题标题】:Google Sheets count number of tables in HTMLGoogle 表格统计 HTML 中的表格数量
【发布时间】:2018-04-20 18:15:58
【问题描述】:

我想将 HTML 页面中的表格数量返回到 Google 表格。下面的代码可以让我知道 chrome 控制台中的表格数量。

var i = 1; [].forEach.call(document.getElementsByTagName("table"),
   function(x) { (i++, x); });
console.log (i)

但我不知道如何在 Google App Script 中获得此结果 (i),因此我可以将其返回到我的工作表中。有点像

function doGet() {
  var html = UrlFetchApp.fetch('http://allqs.saqa.org.za/showUnitStandard.php?id=7743').getContentText();
  var table = getElementsByClassName(html, 'table')[0];

  var i = 1; [].forEach.call(document.getElementsByTagName("table"),
       (i++, x);
    console.log (i)

【问题讨论】:

    标签: html google-apps-script google-sheets


    【解决方案1】:

    您想使用 GAS 从 http://allqs.saqa.org.za/showUnitStandard.php?id=7743 的 URL 中检索 <table> 的标签数量。如果我的理解是正确的,那么这个修改呢?

    修改点:

    • 在原生 GAS 中,getElementsByTagName() 无法使用。
      • 在这个答案中,<table> 的数量是使用正则表达式检索的。

    修改后的脚本:

    var url = "http://allqs.saqa.org.za/showUnitStandard.php?id=7743";
    var res = UrlFetchApp.fetch(url).getContentText();
    var numberOfTables = res.match(/<table/g).length;
    Logger.log(numberOfTables) // 96 is retrieved.
    

    如果我误解了你的问题,我很抱歉。

    【讨论】:

    • @Derek Morgan 欢迎您。我很高兴你的问题得到了解决。也谢谢你。
    猜你喜欢
    • 1970-01-01
    • 2019-12-17
    • 1970-01-01
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    • 2013-11-11
    • 2022-10-18
    • 2020-05-20
    相关资源
    最近更新 更多