首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >批量ImportHTML脚本

批量ImportHTML脚本
EN

Web Applications用户
提问于 2021-12-27 16:20:43
回答 1查看 140关注 0票数 0

在很大的帮助下,这个脚本正在工作,在下面的链接上拉出第二个表格。与第一张表不同的是,IMPORTHTML不能刮表(常规赛有效,季后赛不行)。

https://www.basketball-reference.com/players/c/curryst01/gamelog/2016

代码语言:javascript
复制
function myFunction() {
  const url = "https://www.basketball-reference.com/players/c/curryst01/gamelog/2016"; // This URL is from your question.
  const sheetName = "Sheet1";  // Please set the destination sheet name.

  const html = UrlFetchApp.fetch(url).getContentText();
  const tables = [...html.matchAll(/<table[\s\S\w]+?<\/table>/g)];
  if (tables.length > 2) {
    const ss = SpreadsheetApp.getActiveSpreadsheet();
    Sheets.Spreadsheets.batchUpdate({ requests: [{ pasteData: { html: true, data: tables[8][0], coordinate: { sheetId: ss.getSheetByName(sheetName).getSheetId() } } }] }, ss.getId());

    Sheets.Spreadsheets.batchUpdate({ requests: [{ pasteData: { html: true, data: tables[7][0], coordinate: { sheetId: ss.getSheetByName(sheetName).getSheetId() } } }] }, ss.getId());
    
    
    return;
  }
  throw new Error("Expected table cannot be retrieved.");
}`enter preformatted text here`

现在,我还有两个问题要解决。

  1. 有没有办法调整这段代码,使表8和表7同时加载?我认为这里的代码填充了表8,但随后被表7所取代。我认为是因为批更新?
  2. 是否有办法一次加载多个URL。基本上,我希望复制这段代码,这样50个URL就可以一次加载到表中。

我以前只是使用了一堆IMPORTHTML函数,但是由于季后赛表是Javascript对象,我很难弄清楚它。

EN

回答 1

Web Applications用户

回答已采纳

发布于 2021-12-27 17:48:55

因为代码使用的是batchUpdate,所以您处于正确的轨道上。我建议您花一些时间学习如何在JavaScript中处理数组。也就是说,不需要包含两个batchUpdate,因为可以将两个请求对象放在一个单独的请求对象中,实际上,在一个batchUpdate中可以包含50个pasteData

您的猜测是,两个“粘贴”操作都有效,而第二个覆盖第一个操作是正确的。之所以会发生覆盖,是因为coordinate属性只指定了sheetId,而没有指定rowIndexcolumnIndex属性。

试着替换:

代码语言:javascript
复制
Sheets.Spreadsheets.batchUpdate({ requests: [{ pasteData: { html: true, data: tables[8][0], coordinate: { sheetId: ss.getSheetByName(sheetName).getSheetId() } } }] }, ss.getId());

Sheets.Spreadsheets.batchUpdate({ requests: [{ pasteData: { html: true, data: tables[7][0], coordinate: { sheetId: ss.getSheetByName(sheetName).getSheetId() } } }] }, ss.getId());

通过

代码语言:javascript
复制
Sheets.Spreadsheets.batchUpdate(
{ requests: [
{ pasteData: { html: true, data: tables[8][0], coordinate: { 
sheetId: ss.getSheetByName(sheetName).getSheetId(), 
rowId: 1,
columnId: 1
} } },

{ pasteData: { html: true, data: tables[7][0], coordinate: { 
sheetId: ss.getSheetByName(sheetName).getSheetId(),
rowIndex: 30,
columnIndex: 1
 } } }

] }, ss.getId()
);

完整代码:

代码语言:javascript
复制
function myFunction() {
  const url = "https://www.basketball-reference.com/players/c/curryst01/gamelog/2016"; // This URL is from your question.
  const sheetName = "Sheet1";  // Please set the destination sheet name.

  const html = UrlFetchApp.fetch(url).getContentText();
  const tables = [...html.matchAll(/<table[\s\S\w]+?<\/table>/g)];
  if (tables.length > 2) {
    const ss = SpreadsheetApp.getActiveSpreadsheet();
    Sheets.Spreadsheets.batchUpdate({
      requests: [
        {
          pasteData: {
            html: true, data: tables[8][0], coordinate: {
              sheetId: ss.getSheetByName(sheetName).getSheetId(),
              rowIndex: 1,
              columnIndex: 1
            }
          }
        },
        {
          pasteData: {
            html: true, data: tables[7][0], coordinate: {
              sheetId: ss.getSheetByName(sheetName).getSheetId(),
              rowIndex: 30,
              columnIndex: 1
            }
          }
        }
      ]
    }, ss.getId());


    return;
  }
  throw new Error("Expected table cannot be retrieved.");
}

资源

票数 1
EN
页面原文内容由Web Applications提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://webapps.stackexchange.com/questions/161952

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档