GS
function checker(){ /* ======================================== 初期設定 ======================================== */ const sheet = SpreadsheetApp.getActiveSheet(), startRow = 2,//情報を置くセルの一番左上の列番号(A = 1...) startCol = 6,//情報を置くセルの一番左上の行番号(1 = 1...) URLListRow = startRow - 1,//URLリストの配置した列番号 authInfo = { userName : '',//basic認証を行う場合のユーザーネーム password : ''//basic認証を行う場合のパスワード } /** * 取得するメタ情報 * outName => 取得する情報の名前 好きに設定(空は不可) * name => name or property属性を設定 * prop => 上記nameで設定した属性の値を設定 metaInfo = [ { outName:'keywords', name:'name', prop:'keywords', }, { outName:'desc', name:'name', prop:'description', }, { outName:'og_title', name:'property', prop:'og:title', }, { outName:'og_desc', name:'property', prop:'og:description', }, { outName:'og_url', name:'property', prop:'og:url', }, ] */ /* ======================================== 以下 script ======================================== */ // clear cell for (let i = startRow; i <= sheet.getLastColumn(); i++){ const dataRange = sheet.getRange(startCol,i,sheet.getLastRow()); dataRange.clearContent(); } // create url data const urlData = sheet.getRange(startCol,URLListRow,sheet.getLastRow() - 1).getValues(); let ary = ''; for (var a = 0; a<urlData.length;a++){ if(urlData[a][0]){ ary = ary + String(urlData[a][0]) + ',' } } ary = ary.split(','); ary.pop(); // header option const options = { method: "GET", headers: {"Authorization" : "Basic " + Utilities.base64Encode(authInfo.userName + ":" + authInfo.password)}, "muteHttpExceptions" : true, "validateHttpsCertificates" : false, "followRedirects" : false } let result = []; // get html data for(var i = 0; i < ary.length; i++){ try { let obj = Object.assign({}); let r = Object.assign({}); let resultURL = ary[i]; const res = UrlFetchApp.fetch(resultURL,options); const content = res.getContentText() const $ = Cheerio.load(content); obj.title = $('head title').text(); obj.canonical = $('link[rel="canonical"]').attr('href'), obj.stylesheet = $('link[rel="stylesheet"]').attr('href'), /* metaInfo.forEach((v)=>{ const reg = new RegExp(v.prop,'g'); if(content.match(reg)){ obj[v.outName] = $(`meta[${v.name}="${v.prop}"]`).attr("content"); }else{ obj[v.outName] = false; } }) */ r.props = Object.assign({},obj); result.push(r); obj = null; r = null; //sleep 1秒 Utilities.sleep(1000); } catch(e) { // 例外エラー処理 console.log('Error:'+e); } } // draw cell result.forEach((d,r)=>{ Object.keys(d.props).forEach((k,i)=>{ const row = startCol + r; const col = startRow + i; sheet.getRange(row, col).setValue(d.props[k]); }) }) }
0 コメント