function onChangeCell() {const mySheet = SpreadsheetApp.getActiveSheet();const myActiveCell = mySheet.getActiveCell();const selectedRow = myActiveCell.getRow();const selectedColumn = myActiveCell.getColumn();const infoKeyRow = 1;const key = mySheet.getRange(infoKeyRow, selectedColumn).getValue();if (key=="video_id" || key=="watch_url") {const smid = matchSmid(myActiveCell.getValue());if (!smid) return;const statusCol = 1;mySheet.getRange(selectedRow, statusCol).setValue("処理中…");try {const info = getThumbInfo(smid);const infoKeys = getInfoKeys(infoKeyRow)setInfoToActiveRow(infoKeys, info);mySheet.getRange(selectedRow, statusCol).setValue("完了");} catch(e) {mySheet.getRange(selectedRow, statusCol).setValue(e);}}}function getThumbInfo(smid){const url = "http://ext.nicovideo.jp/api/getthumbinfo/" + smid;const options = {'method': 'get',};const response = UrlFetchApp.fetch(url, options);const xml = XmlService.parse(response.getContentText());const thumb = xml.getRootElement().getChildren("thumb")[0];if (!thumb) {throw xml.getRootElement().getChildren("error")[0].getChildText("code");}const info = {};info["update_at"] = Utilities.formatDate(new Date(), 'Asia/Tokyo', 'yyyy-MM-dd HH:mm:ss');info["video_id"] = thumb.getChildText("video_id");info["watch_url"] = thumb.getChildText("watch_url");info["title"] = thumb.getChildText("title");info["description"] = thumb.getChildText("description");info["thumbnail_url"] = thumb.getChildText("thumbnail_url");info["thumbnail"] = getImage(info["thumbnail_url"]);info["first_retrieve"] = thumb.getChildText("first_retrieve");info["length"] = thumb.getChildText("length");info["view_counter"] = thumb.getChildText("view_counter");info["comment_num"] = thumb.getChildText("comment_num");info["mylist_counter"] = thumb.getChildText("mylist_counter");info["last_res_body"] = thumb.getChildText("last_res_body");info["embeddable"] = thumb.getChildText("embeddable");info["no_live_play"] = thumb.getChildText("no_live_play");const tagEls = thumb.getChildren("tags")[0].getChildren("tag");let tag_str = "";for(let i = 0; i < tagEls.length; i++) {tag_str += tagEls[i].getText() + " ";}info["tags"] = tag_str;info["genre"] = thumb.getChildText("genre");info["user_nickname"] = thumb.getChildText("user_nickname");info["user_id"] = thumb.getChildText("user_id");info["user_icon_url"] = thumb.getChildText("user_icon_url");info["user_icon"] = getImage(info["user_icon_url"]);//console.log(response.getContentText());return info;}function getSmidFromActiveCell() {const mySheet = SpreadsheetApp.getActiveSheet();const myActiveCell = mySheet.getActiveCell();const selectedRow = myActiveCell.getRow();const selectedColumn = myActiveCell.getColumn();const smid = matchSmid(mySheet.getRange(selectedRow, selectedColumn).getValue());if (smid){return smid;}return false;}function getInfoKeys(infoKeyRow) {const mySheet = SpreadsheetApp.getActiveSheet();const lastColumn = mySheet.getLastColumn();const infoKeys = [];for(let i = 1; i <= lastColumn; i++) {//console.log(mySheet.getRange(infoKeyRow, i).getValue());infoKeys[i]= mySheet.getRange(infoKeyRow, i).getValue();}return infoKeys;}function setInfoToActiveRow(infoKeys, info) {const mySheet = SpreadsheetApp.getActiveSheet();const myActiveCell = mySheet.getActiveCell();const selectedRow = myActiveCell.getRow();const lastColumn = mySheet.getLastColumn();for(let i = 1; i <= lastColumn; i++) {const value = info[infoKeys[i]];if(value!=null){mySheet.getRange(selectedRow, i).setValue(value);}}}function matchSmid(text){const m = text.match(/(sm\d+)/)if (!m) return false;return m[0];}function getImage(url) {const formula = `=IMAGE("${url}",1)`;return formula}
スクリプトエディタの上の所らへんのボタンアイコン並んでる所にある時計みたいなマーク押す(現在のプロジェクトのトリガーを開く)
右下のトリガーを追加押して
実行する関数 onChangeCell
イベントのソース スプレッドシートから
イベントの種類 変更時
に設定して保存
スプレッドシートに戻り、一番上の行の1列目を除く各列に好きな順で「video_id」や「watch_url」など(キー名、後述)を入力していく
これで設定完了
これで、video_id又はwatch_urlの列にsm〇〇を含む文字列を入れて確定すると
自動的にその行の各列に取得した動画情報が入る。
使える動画情報の種類(一番上の行に入れるキー名)はコードを参照してください
コードにある、info["update_at"]とかinfo["user_icon"]とか書いてる所の「update_at」とか「user_icon」とかです。
設定が完了したスプレッドシートのコピーを作成すると便利(スプレッドシートのメニューから「ファイル->コピーを作成」)
初回実行時に「アカウントへのアクセスをリクエストしています」とか出ると思うが、許可していく
「このアプリがGoogleで確認されていません」が出たら、「詳細を表示」を押して、「~~(安全ではないページ)に移動」を押すと、出来る。
コードは自由に改変再配布して大丈夫です。
今の所、複数セルをまとめて変更した時などは左上のセルしか反応しないと思います。
複数まとめて動作も出来ないか後で試します。
0 件のコメント:
コメントを投稿