【问题标题】:xpath for importxml in google sheet or importing by GAS谷歌表中 importxml 的 xpath 或通过 GAS 导入
【发布时间】:2021-04-16 18:47:35
【问题描述】:

【问题讨论】:

    标签: xml google-apps-script xpath google-sheets google-sheets-formula


    【解决方案1】:

    我相信你的目标如下。

    • 您要检索https://codal.ir/Reports/Decision.aspx?LetterSerial=edVsdLrjCqoJOnLY0QVskw%3D%3D&rt=2&let=8&ct=0&ft=-1 的URL 表。
    • 您希望使用 IMPORTXML 和/或 Google Apps 脚本来实现此目的。

    我认为在这种情况下,如何使用IMPORTHTML如下?

    使用公式的示例:

    =IMPORTHTML(A1,"table",1)
    
    • 在这种情况下,https://codal.ir/Reports/Decision.aspx?LetterSerial=edVsdLrjCqoJOnLY0QVskw%3D%3D&rt=2&let=8&ct=0&ft=-1 的 URL 放在单元格“A1”中。
    结果:

    使用 Google Apps 脚本的示例:

    使用 Google Apps 脚本时,也可以使用以下脚本。 When you use this script, please enable Sheets API at Advanced Google services。并且,请使用脚本编辑器运行myFunction 的功能。因为在这种情况下,Sheets API 用于解析 HTML 表格。不幸的是,这不能直接与自定义函数一起使用。运行此函数时,会将值放入活动工作表中。

    function myFunction() {
      const url = "https://codal.ir/Reports/Decision.aspx?LetterSerial=edVsdLrjCqoJOnLY0QVskw%3D%3D&rt=2&let=8&ct=0&ft=-1";
      const res = UrlFetchApp.fetch(url, {muteHttpExceptions: true});
      if (res.getResponseCode() != 200) throw new Error(res.getContentText());
      const table = res.getContentText().match(/<table[\s\S\w]+<\/table>/);
      if (table) {
        const spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
        const sheet = spreadsheet.getActiveSheet();
        const resource = {requests: [{pasteData: {html: true, data: table[0], coordinate: {sheetId: sheet.getSheetId()}}}]};
        Sheets.Spreadsheets.batchUpdate(resource, spreadsheet.getId());
      }
    }
    
    结果:

    注意:

    • 我在测试上面的公式时,有经验出现Could not fetch url的错误。所以请注意这一点。届时,请尝试重新输入公式。通过这个,我可以获得这些值。不幸的是,我无法理解其中的详细原因。

    参考资料:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多