简体   繁体   English

谷歌表格,应用程序脚本 parsehtml 错误

[英]google sheets, apps script parsehtml error

this is the complete code,这是完整的代码,

function extractData() {
  var url = "https://www.theopenalliance.com/teams/2023/";
  var html = UrlFetchApp.fetch(url).getContentText();
  var data = parseHtml(html);
  var sheet = SpreadsheetApp.getActiveSheet();
  sheet.clearContents();
  if (data.length > 0) {
    sheet.getRange(1, 1, data.length, data[0].length).setValues(data);
    for (var i = 0; i < data.length; i++) {
      for (var j = 0; j < data[i].length; j++) {
        if (data[i][j].indexOf("http") === 0) {
          var button = sheet.getRange(i + 1, j + 1).attachButton({
            text: "Link",
            url: data[i][j]
          });
        }
      }
    }
  }
}

function parseHtml(html) {
  var startIndex = html.indexOf("<tbody>");
  var endIndex = html.indexOf("</tbody>");
  var table = html.substring(startIndex, endIndex);
  var rows = table.split("<tr>");
  var data = [];
  for (var i = 1; i < rows.length; i++) {
    var cells = rows[i].split("<td");
    var row = [];
    for (var j = 1; j < cells.length; j++) {
      var cell = cells[j];
      var linkStartIndex = cell.indexOf("href=");
      if (linkStartIndex !== -1) {
        var linkEndIndex = cell.indexOf("class");
        var link = cell.substring(linkStartIndex + 6, linkEndIndex - 2);
        row.push(link);
      } else {
        row.push(cell.substring(cell.indexOf(">") + 1, cell.indexOf("</td>")));
      }
    }
    data.push(row);
  }
  return data;
} 

however function parseHtml(html) gives an error with this line然而 function parseHtml(html)给出了这一行的错误

var startIndex = html.indexOf("<tbody>");

Anyone has any suggestions?有人有什么建议吗? i'm trying to copy and paste tables from the link to a google sheets.我正在尝试将表格从链接复制并粘贴到谷歌表格。

i expected to see every teams numbers and other values (Public links, location etc) in google sheets but nothing shows up.我希望在谷歌表格中看到每个团队的号码和其他值(公共链接、位置等),但什么也没有显示。 Also i was expecting to see buttons that had links attached to them if the buttons exists, such as github, photos etc. Please check the link and im sure you will have a better idea of im trying to tell.如果按钮存在,例如 github、照片等,我还希望看到附有链接的按钮。请检查链接,我相信您会对我试图讲述的内容有更好的了解。 Also please help me fix the code, if possible, copy and edit the code than repost it, i would greatly appreciate it也请帮我修复代码,如果可能的话,复制并编辑代码而不是重新发布,我将不胜感激

In your situation, how about using Sheets API?在您的情况下,使用 Sheets API 怎么样? Because I thought that the HTML parser of Sheets API is useful for your situation.因为我认为 Sheets API 的 HTML 解析器对您的情况很有用。 When Sheets API is used for your URL, how about the following sample script?当Sheets API用于你的URL时,下面的示例脚本怎么样?

Sample script:示例脚本:

Before you use this script, please enable Sheets API at Advanced Google services .在使用此脚本之前, 请在 Advanced Google services 中启用 Sheets API

function myFunction() {
  const url = "https://www.theopenalliance.com/teams/2023/"; // This is from your script.

  const html = UrlFetchApp.fetch(url).getContentText();
  const table = html.match(/<table[\s\S\w]+?<\/table>/);
  if (!table) {
    throw new Error("Table was not found.");
  }
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sheet = ss.getActiveSheet().clearContents();
  SpreadsheetApp.flush();
  const requests = { requests: [{ pasteData: { html: true, data: table[0], coordinate: { sheetId: sheet.getSheetId() } } }] };
  Sheets.Spreadsheets.batchUpdate(requests, ss.getId());
}
  • When this script is run, a table is retrieved from the URL and put it into the active sheet.运行此脚本时,会从 URL 中检索一个表并将其放入活动表中。

References:参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2025 STACKOOM.COM