我想每天使用 Google 表格中的 IMPORTDATA 功能自動抓取資料。我正在使用基于 URL 的 API。我的問題是在 URL 的結構中必須插入某個日期。我不知道如何將日期變成一個變數,以便 URL 每天自動更改。
昨天資料的 URL 是:https ://www.benzinpreis.de/statistik.phtml?o=4&so=b.order_total&cnt=50&mystatart=LKR&mystat=DAY13.11.2022 00:00:00
我的代碼現在看起來像這樣:
function TagesdurchschnittDeutschland() {
let yesterday = new Date()
yesterday.setDate(yesterday.getDate()-1);
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.setActiveSheet(spreadsheet.getSheetByName('TagesdurchschnittDeutschland'))
spreadsheet.getRange('A1').activate()
.setFormula('=IMPORTHTML("https://www.benzinpreis.de/statistik.phtml?o=4&so=b.order_total&cnt=50&mystatart=LKR&mystat=DAY' yesterday.getDate() '.' yesterday.getMonth() '.' yesterday.getFullYear() ' 00:00:00"; "table"; 5)');
}
uj5u.com熱心網友回復:
此功能將回傳昨天的URL。它取決于為應用程式腳本定義的時區,您可以在腳本的全域設定中進行更改。
function makeYesterdayUrl() {
let yesterday = new Date()
yesterday.setDate(yesterday.getDate()-1);
return 'https://www.benzinpreis.de/statistik.phtml?o=4&so=b.order_total&cnt=50&mystatart=LKR&mystat=DAY'
yesterday.getDate().toString().padStart(2, '0')
'.'
(yesterday.getMonth() 1).toString().padStart(2, '0')
'.'
yesterday.getFullYear()
' 00:00:00'
}
注意我自己,永遠不要忘記getMonth從1(1月)開始的,而不是1 ...(參考)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/533347.html
上一篇:在谷歌表格中按組查找最大值
