반응형
💡 GCP 에서 제공하는 최신 리전 및 영역 데이터를 Google Sheets로 가져오고 싶다.
주, 혹은 월단위로 동기화를 유지하고 싶다.
아래 사이트에서 GCP 의 region 정보들을 Google Sheets로 가져오고 싶다.
그리고 region이 추가 되면, 자동으로 Google Sheets에 region이 추가되면 좋겠다.
리전 및 영역 | Compute Engine 문서 | Google Cloud
의견 보내기 리전 및 영역 컬렉션을 사용해 정리하기 내 환경설정을 기준으로 콘텐츠를 저장하고 분류하세요. Compute Engine 리소스는 전 세계 여러 곳에서 호스팅됩니다. 이러한 위치는 리전과 영
cloud.google.com
크롤링을 위해 Cheerio 라이브러리를 등록한다.
(Cheerio 라이브러리 등록하는법)
Apps Script로 어제자 네이버 증권 시황 뉴스를 cheerio를 이용해 크롤링 해오기
하나 하나 붙이다보니 욕심이 생겼다. 매일아침 출근길에 증시관련 뉴스를 헤드라인만 받아보고 싶었다. python에서 크롤링은 해봤는데, apps script에서도 시도해 보았다. https://finance.naver.com/news/mai
whiseung.tistory.com
데이터를 가져와 뿌려줘 본다. 정상적으로 나오는 걸 확인 할 수 있다.
function getGcpRegionInfo () {
const url = 'https://cloud.google.com/compute/docs/regions-zones';
const content = UrlFetchApp.fetch(url).getContentText();
const $ = Cheerio.load(content);
$("tbody.list > tr").each((index, element) => {
console.log($($(element).find("td")[0]).text() + " / " + $($(element).find("td")[1]).text());
});
}
이제 데이터를 Google Sheets에 뿌려줘 본다.
function getGcpRegionInfo () {
const url = 'https://cloud.google.com/compute/docs/regions-zones';
const content = UrlFetchApp.fetch(url).getContentText();
const $ = Cheerio.load(content);
const collections = [];
$("tbody.list > tr").each((index, element) => {
let arr = new Array();
arr.push($($(element).find("td")[0]).text());
arr.push($($(element).find("td")[1]).text());
arr.push($($(element).find("td")[2]).text());
arr.push($($(element).find("td")[3]).text());
arr.push($($(element).find("td")[4]).text());
collections.push(arr);
});
console.log(collections);
// google sheets 정보
const sheetId = "google_sheets_id";
const sheetName = "locations";
const sheet = SpreadsheetApp.openById(sheetId).getSheetByName(sheetName);
const range = sheet.getRange("A:F");
// 데이터 뿌리기
sheet.getRange("A1:F" + (collections.length)).setValues(collections);
}
헤더가 없다.
헤더를 추가하고, 데이터를 뿌려준다.
필요한 데이터가 있어 한 줄 더 가공해 뿌려준다.
function getGcpRegionInfo () {
const url = "https://cloud.google.com/compute/docs/regions-zones";
const content = UrlFetchApp.fetch(url).getContentText();
const $ = Cheerio.load(content);
const collections = [];
// 타이틀 추출
$("thead > tr").each((index, element) => {
let arr = new Array();
arr.push($($(element).find("th")[0]).text());
arr.push($($(element).find("th")[1]).text());
arr.push($($(element).find("th")[2]).text());
arr.push($($(element).find("th")[3]).text());
arr.push($($(element).find("th")[4]).text());
collections.push(arr);
});
// 테이블 데이터 추출
$("tbody.list > tr").each((index, element) => {
const zone = $($(element).find("td")[0]).text();
const location = $($(element).find("td")[1]).text();
// Zone 헤더 생성
if ( zone.indexOf("-a") > 0 ) {
let arr = new Array();
arr.push(zone.replace("-a", ""));
arr.push(location);
arr.push("");
arr.push("");
arr.push("");
arr.push("");
collections.push(arr);
}
let arr = new Array();
arr.push(zone);
arr.push(location);
arr.push($($(element).find("td")[2]).text());
arr.push($($(element).find("td")[3]).text());
arr.push($($(element).find("td")[4]).text());
arr.push($($(element).find("td")[5]).text());
collections.push(arr);
});
// google sheets 정보
const sheetId = "google_sheets_id";
const sheetName = "locations";
const sheet = SpreadsheetApp.openById(sheetId).getSheetByName(sheetName);
const range = sheet.getRange("A:F");
// 기존 데이터 지우기
console.log("기존 데이터 지우기");
range.clearContent();
// 데이터 뿌리기
sheet.getRange("A1:F" + (collections.length)).setValues(collections);
}
이제 생성한 함수를 주 단위 혹은 월 단위 트리거를 걸어 놓으면,
알아서 동기화를 진행할 것이다.
반응형
'GCP > Apps Script' 카테고리의 다른 글
Apps Script로 Google Sheets의 범위 이름 설정을 이용해 셀 관리하기 (0) | 2023.04.12 |
---|---|
Apps Script로 Google Sheets에서 상품 관리 sheet 만들기 (0) | 2023.04.11 |
Apps Script로 웹사이트에 업로드 되어있는(a tag) CSV 파일을 Google Sheets로 불러오기 (0) | 2023.03.28 |
Apps Script로 Google Sheets의 값들을 Insert 문으로 생성 (0) | 2023.03.27 |
Apps Script로 URL호출을 통한 이메일 보내기 (API처럼 사용하기) (0) | 2023.03.17 |