前提
・GAS実行方法を知っている
・LOLのJSON取得URLを知っている
知らない人は以下参考
チャンピオン一覧の取得方法は以下の通りです。
function getChampionNames() {
var url = "https://ddragon.leagueoflegends.com/cdn/13.1.1/data/ja_JP/champion.json";
var response = UrlFetchApp.fetch(url);
var json = JSON.parse(response.getContentText());
var championData = json.data;
var names = [];
for (var key in championData) {
names.push(championData[key].name);
}
console.log(names);
return names;
}
応用編(チャンピオン達のスキルを取得してみよう)
const VERSION = "13.1.1";
const URL = `https://ddragon.leagueoflegends.com/cdn/${VERSION}/data/ja_JP/`;
function createChampionSheet() {
// Get Champion IDs from the API
const championIds = getChampionIds();
console.log("Champion IDs: ", championIds);
// Loop through each champion ID to get their data
for (const championId of championIds) {
myFunction(championId);
}
}
function getChampionIds() {
// Fetch Champion data from the API
const url = `${URL}champion.json`;
const json = UrlFetchApp.fetch(url).getContentText();
const data = JSON.parse(json);
const championData = data.data;
// Extract champion IDs from the data
const championIds = Object.keys(championData);
console.log("Champion IDs: ", championIds);
return championIds;
}
function myFunction(championId) {
// Fetch champion data from the API
const url = `${URL}champion/${championId}.json`;
const response = UrlFetchApp.fetch(url);
const data = JSON.parse(response.getContentText());
console.log("Champion Data: ", data);
// Get the active spreadsheet
const spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
// Create sheet if it doesn't exist, otherwise get the sheet
let sheet = spreadsheet.getSheetByName(championId);
if (!sheet) {
sheet = spreadsheet.insertSheet(championId);
}else{
// Clear the sheet
sheet.clear();
}
const champion = data.data[championId];
// Get champion skills data
const skills = champion.spells;
console.log("Champion Skills1: ", skills);
// Prepare data for writing to sheet
const skillName = [];
const skillDescription = [];
const skillImageUrl = [];
for (const skill of skills) {
skillName.push(skill.name);
skillDescription.push(skill.description);
skillImageUrl.push(
`=Image("https://ddragon.leagueoflegends.com/cdn/${VERSION}/img/spell/${skill.image.full}")`
);
console.log("Champion Skills2: ", skills);
}
// Write data to sheet
sheet.appendRow([champion.name, champion.title, champion.blurb]);
sheet.appendRow(["スキル名", "スキルの説明", "スキルの画像URL"]);
for (let i = 0; i < skills.length; i++) {
sheet.appendRow([skillName[i], skillDescription[i].replace(/<br>/g, "\n"), skillImageUrl[i]]);
console.log("Champion Skills3: ", skills);
}
}