0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

LOLチャンピオン一覧を取得してみよう

Last updated at Posted at 2023-01-29

前提
・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);
    }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?