我在 Google Apps Scripts 中有一個代碼,它采用 Spotify URL 并將關注者的數量粘貼到您參考該 URL 的單元格中。=SAMPLE(A2)(其中 A2 保存 URL)
看來規范又變了。而且我不確定如何解決它。任何指導或幫助將不勝感激!
這是原始帖子 = FetchingURL Using JSON on Google Sheets
這是我目前在 Google Apps Scripts 中的代碼。(@Tanaike 提供)
function SAMPLE(url) {
const res = UrlFetchApp.fetch(url).getContentText();
const v = res.match(/"followers":({[\s\S\w] ?})/);
return v && v.length == 2 ? JSON.parse(v[1].trim()).total : "Value cannot be
retrieved.";
}
謝謝!
uj5u.com熱心網友回復:
在當前階段,JSON資料似乎被放置為base64資料,并且物件的結構也發生了變化。那么,作為當前的示例腳本,下面的示例腳本呢?
示例腳本:
function SAMPLE(url) {
const res = UrlFetchApp.fetch(url).getContentText();
const v = res.match(/<script id\="initial-state" type\="text\/plain">([\s\S\w] ?)<\//);
if (!v || v.length != 2) return "Value cannot be retrieved.";
const obj = JSON.parse(Utilities.newBlob(Utilities.base64Decode(v[1].trim())).getDataAsString());
const value = Object.entries(obj.entities.items).reduce((n, [k, o]) => {
if (k.includes("spotify:playlist:")) n.push(o.followers.total);
return n;
}, []);
return value.length > 0 ? value[0] : "Value cannot be retrieved.";
}
筆記:
- 此示例腳本適用于當前的 HTML 資料。所以,我認為這可能會在未來的更新中改變。因此,我還想建議您將 API 用于您未來的作業,因為它已經在評論中提到。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/504970.html