【发布时间】:2023-04-05 18:09:01
【问题描述】:
我对 Javascript 和 Google Apps 脚本还很陌生,我正在尝试创建一个脚本,它可以获取您朋友的 Steam ID、循环播放他们拥有的游戏、将它们列在电子表格中,并显示某人是否拥有游戏与否。
我已经完成了第一部分,循环遍历每个 ID 的所有拥有的游戏,如果它们在数组中不存在,则将它们添加到数组中完美地使用:
var steamId = ['SomeSteamIDs'];
var gameIds = [];
var games = [];
function getGames(){
for (var i = 0; i < steamId.length; i++){
var response = UrlFetchApp.fetch("http://api.steampowered.com/IPlayerService/GetOwnedGames/v0001/?key=**YourSteamKey**&steamid=" + steamId[i] + "&format=json&include_appinfo=1");//Steam URL.
Logger.log('Response Code: ' + response.getResponseCode());//Checks the request actually connected.
var data = JSON.parse(response.getContentText());//Gets the plaintext JSON response and converts it to an object.
for (var n = 0; n < data.response.games.length; n++){//For the length of the games list
var code = data.response.games[n].appid;
var name = data.response.games[n].name;
if (gameIds.indexOf(code) === -1){//if the AppID doesn't appear in the 'appId' array and sub-arrays
gameIds[gameIds.length] = code;//then put it in the appId array for comparison
games[games.length] = [code,name];// and add the AppId and Game to the games array for writting.
};
};
}
var range = sheet.getRange("A2:B" + (gameIds.length + 1));//+1 here to compensate for starting on line 2, instead of 1.
range.setValues(games);//Perform one write action
}
这在编译所有 SteamID 拥有的游戏主列表时非常有效,但我很难找到一种方法来检查每个个人 ID 拥有哪些游戏,哪些不是。
最初,我尝试在运行“getGames”函数时将“是/否”字符串添加到“游戏”数组中,但我提出的任何解决方案都会丢失数据。如果我过早比较这些值,'gameIds' 数组不会包含所有数据,因此 first SteamID 错过了与 last SteamID 拥有。
如果我做得太晚,“数据”变量只包含它检查的 last SteamID 的响应数据,所以我错过了检查第一个的游戏> SteamID 拥有。
我现在已经多次阅读How to compare arrays in JavaScript? 的答案,但我仍在尝试确定是否可以使用它来解决我的问题。
有没有办法让我实现我正在寻找的东西,最有效的方法是什么?
【问题讨论】:
标签: javascript google-apps-script steam