在很大的帮助下,这个脚本正在工作,在下面的链接上拉出第二个表格。与第一张表不同的是,IMPORTHTML不能刮表(常规赛有效,季后赛不行)。
https://www.basketball-reference.com/players/c/curryst01/gamelog/2016
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`现在,我还有两个问题要解决。
我以前只是使用了一堆IMPORTHTML函数,但是由于季后赛表是Javascript对象,我很难弄清楚它。
发布于 2021-12-27 17:48:55
因为代码使用的是batchUpdate,所以您处于正确的轨道上。我建议您花一些时间学习如何在JavaScript中处理数组。也就是说,不需要包含两个batchUpdate,因为可以将两个请求对象放在一个单独的请求对象中,实际上,在一个batchUpdate中可以包含50个pasteData。
您的猜测是,两个“粘贴”操作都有效,而第二个覆盖第一个操作是正确的。之所以会发生覆盖,是因为coordinate属性只指定了sheetId,而没有指定rowIndex和columnIndex属性。
试着替换:
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());通过
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()
);完整代码:
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.");
}资源
https://webapps.stackexchange.com/questions/161952
复制相似问题