今回行うこと
前回スプレッドシートに書き込んだ内容に
1行毎に別の背景色を設定して表を見やすくしたいと思います。
前回の記事はこちら
色が分かりやすいように設定してみましたが、目が痛いですねw
↓ 今回のゴール
背景色を交互に設定するコード
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
function Gas_WebScraping06() { /*********************** * スプレッドシートの指定 交互に色付けを行う ***********************/ // 現在開いているスプレッドシートを読み込む let active_sheet01 = SpreadsheetApp.getActiveSpreadsheet(); active_sheet01 = active_sheet01.getActiveSheet(); active_sheet01.clearFormats(); // タウンワークのURLを指定(条件までは手動でクリックした状態) let get_url = "https://townwork.net/hokkaidou/ct_ma10100/jc_005/?ds=04"; // getContentTest()のデフォルト指定は(utf-8)になるので省略 let get_html = UrlFetchApp.fetch(get_url).getContentText(); let Array01 = [] let Array02 = [] // 社名と業務名の取得から、社名だけ切り分け用 Array01 = Parser.data(get_html).from('job-lst-main-ttl-txt').to('</h3>').iterate(); // 社名と業務名の取得から、業務名だけ切り分け用 Array02 = Parser.data(get_html).from('job-lst-main-txt-lnk').to('</p>').iterate(); /********************************************* * 社名だけ切り分けを行う *********************************************/ for(let x = 0; x < Array01.length; x++){ // 不要な文字列を削除 Array01[x] = Array01[x].replace(/".*>/,"") // 不要な空白を削除 active_sheet01.getRange(x+1,1).setValue(Array01[x].trim()); } /********************************************* * 業務名だけ切り分けを行う *********************************************/ for(let x = 0; x < Array02.length; x++){ // 不要な文字列を削除 Array02[x] = Array02[x].replace(/".*>/,"") // 不要な空白を削除 active_sheet01.getRange(x+1,2).setValue(Array02[x].trim()); } /********************************************* * 交互に背景色を設定する *********************************************/ for(let x = 1; x <= active_sheet01.getLastRow(); x++){ if(x % 2 == 0){ active_sheet01.getRange(x, 1, 1, active_sheet01.getLastColumn()).setBackgroundColor('lime'); } else{ active_sheet01.getRange(x, 1, 1, active_sheet01.getLastColumn()).setBackgroundColor('aqua'); } } } |
解説
以下の部分が今回追加したコードです。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
/********************************************* * 交互に背景色を設定する *********************************************/ for(let x = 1; x <= active_sheet01.getLastRow(); x++){ if(x % 2 == 0){ active_sheet01.getRange(x, 1, 1, active_sheet01.getLastColumn()).setBackgroundColor('lime'); } else{ active_sheet01.getRange(x, 1, 1, active_sheet01.getLastColumn()).setBackgroundColor('aqua'); } |
処理の手順
・値が入っている行数までループを回す
・スタートは1行目:値が入っている列(B列)を対象とする
・setBackgroundColorで背景色を設定(引数は16進数でもOK)
となります。
今回は以上となります。
最後までお読みいただきありがとうございました!