我在 StackOverflow 上找到了一個解決方案,我一直在使用它來填充某些網站的表格資料,并且腳本沒有任何問題(包括我會根據網站進行的一些修改等......)但是,我盡管我看到了來自該網站的回應,但無法從該網站獲取資料以進行填充Logger.log(table)
這是因為table不是有效的 JSON 嗎?有沒有辦法修改腳本來提取這些資料?
function senate() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
const url = 'html';
const sheetName = "senate";
var dstSheet = ss.getSheetByName("senate");
var lr = dstSheet.getLastRow();
const html = UrlFetchApp.fetch(url).getContentText();
const tables = [...html.matchAll(/<table[\s\S\w] ?<\/table>/g)];
Logger.log(tables)
if (tables !== null) {
const ss = SpreadsheetApp.getActiveSpreadsheet();
Sheets.Spreadsheets.batchUpdate({ requests: [{ pasteData: { html: true, data: tables, type: "PASTE_VALUES", coordinate: { sheetId: ss.getSheetByName(sheetName).getSheetId(),rowIndex: lr, columnIndex: 0 } } }] }, ss.getId());
return;
}
throw new Error("Expected table cannot be retrieved.");
}
uj5u.com熱心網友回復:
如果您使用的是這個示例腳本,我認為在這種情況下,tables它是一個陣列。那么,下面的修改呢?
從:
Sheets.Spreadsheets.batchUpdate({ requests: [{ pasteData: { html: true, data: tables, type: "PASTE_VALUES", coordinate: { sheetId: ss.getSheetByName(sheetName).getSheetId(),rowIndex: lr, columnIndex: 0 } } }] }, ss.getId());
至:
Sheets.Spreadsheets.batchUpdate({ requests: [{ pasteData: { html: true, data: tables[0][0], type: "PASTE_VALUES", coordinate: { sheetId: ss.getSheetByName(sheetName).getSheetId(),rowIndex: lr, columnIndex: 0 } } }] }, ss.getId());
- 當我看到
tables.length時,它是 1。所以,在這個修改中,data: tables被修改為data: tables[0][0].
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/517653.html
上一篇:有沒有辦法將一個函式中定義的隨機變數傳遞給Python中的另一個函式?
下一篇:谷歌表格,查詢,按月選擇
