๐ก Apps Script ๋ฅผ ์ด์ฉํด ์ค์ ๋ก ๊ฐ๋ฐํ๋ ๋ด์ฉ์ ์์ฝํ๋ค.
์์์ Apps Script์ ๊ตฌ์กฐ๋ฅผ ๊ฐ๋ตํ ์ค๋ช ํ๋๋ฐ,
์ฌ์ฉ ์์์ ํจ๊ป ์ข ๋ ๋ํ ์ผํ๊ฒ ์์ฑํด ๋ณด์๋ค.
์์ธ ์์ ๋ถ์ Google Sheet์ ์ฐ๊ฒฐ ๋ Apps Script (๊ป๋ฐ๊ธฐ)
Google Sheets ๋ฉ๋ด์์ [ํ์ฅํ๋ก๊ทธ๋จ] - [Apps script] ๋ก ์์ฑ๋ Apps Script ์ด๋ค.
์๋จ ๋ณ์์์,
"๋ด๊ฐ ์์ธ์์ ๋ถ์์ ๊ตฌ๊ธ์ํธ ํ์ผ์ด๋ค." ๋ผ๊ณ ๊ตฌ๋ถ์ง์์ ์๋,
sheet url๊ณผ code๋ฅผ ์ ์ธํด์ฃผ๊ณ ,
accountLib ์ ํด๋น ์ ๋ณด๋ค์ ์ ๋ฌํด์ค๋ค.
const this_url = accountLib.currentSheetUrl();
const this_jisaCode = "1000";
/**
* Generate Top buttons.
*/
function onOpen() {
SpreadsheetApp.getUi()
.createMenu('๐ ๋ฆฌ์คํธ')
.addItem('๐ ์กฐํ', 'getList')
.addToUi();
}
/**
* ๋ฐ์ดํฐ ์กฐํ
*/
function getList() {
accountLib.getList(this_url, this_jisaCode);
}
์์ ๊ณตํต Apps script
์ง์ฌ๋ณ ์์ Google Sheet ํ ํ๋ฆฟ์ ๋์ผํ ๊ฒ์ด๋ค.
๊ทธ๋ ๊ธฐ ๋๋ฌธ์, ์์ ๊ณตํต apps script ํ์ผ์์
sheet ์ด๋ฆ๊ณผ dataset, table์ด๋ฆ์ ์ ์ธํด์ค๋ค.
๊ทธ๋ฆฌ๊ณ ๋ฐ์ดํฐ๋ฅผ ์กฐํํด์ ๋ฟ๋ ค์ค ์ ๋ณด๋ค์ common apps script์
queryAndBinding ๋ผ๋ ํจ์๋ก ์ ๋ฌํ๋ค.
const listSheetName = "๋ฟ๋ ค์ค ์ํธ ์ด๋ฆ";
const dataTableId = "๊ฐ์ ธ์ฌ ํ
์ด๋ธ ์ด๋ฆ";
/**
* ์คํ๋ sheet url return
*/
function currentSheetUrl () {
return common.getSheetUrl();
}
/**
* ๋ฆฌ์คํธ ์กฐํํ๊ธฐ
* @param {string} sheetUrl = ํด๋น function์ ํธ์ถํ sheet์ URL
* @param {string} jisa_code = ํด๋น function์ ํธ์ถํ sheet์ ์ฌ์
์ฅ์ฝ๋
*/
function getA4Data(sheetUrl, jisa_code ) {
try {
const sheetName = listSheetName ;
const datasetId = GLOBAL_DATASET_ID;
const tableId = dataTableId;
const sheet = SpreadsheetApp.openByUrl(sheetUrl).getSheetByName(sheetName);
// ์กฐํ์กฐ๊ฑด
const cellId = "C4";
const target_year = common.getCellDataBySheet(sheet, cellId);
if ( !target_year ) { common.alertMissParam(cellId); return; }
var columnName = 'grn_class, detail_class, unit, landfill_flag';
var whereState = 'AND year_date = "' + target_year + '"';
if ( !common.isNull(jisa_code ) ) {
whereState += ' AND jisa_code = "' + jisa_code + '" ';
}
const orderState = ' ORDER BY CAST(unit AS int64) DESC ; ';
const queryString = 'SELECT ' + columnName + ' FROM `'
+ GLOBAL_PROJECT_ID + "." + datasetId + "." + tableId + '` '
+ ' WHERE status = "Y" ' + whereState + orderState;
const dataRange = common.queryAndBinding(sheetUrl, sheetName, queryString, 10, 1, "N", null, "N");
if ( !common.isNull(dataRange) ) {
common.setTableBorderBySheet(sheet, dataRange, true);
}
} catch (error) {
var msg01 = error.message;
common.alertMessage("โ Error", "(getList) Error Message : " + msg01);
Logger.log(msg01);
}
}
๊ณตํต Apps script
๋ชจ๋ ์ ๋ณด๋ค์ ๋ฐ์์ ์ ๋ง ์คํ๋ง ํด์ฃผ๋ ์คํ๋ถ์ด๋ค.
/**
* ๋ฐ์ดํฐ๋ฅผ Bigquery์์ ์กฐํํด์ ์คํ๋ ๋ ์ํธ์ ํ์ถ
* @param {string} sheetUrl = ์์
ํ sheet URL
* @param {string} sheetName = ์์
ํ sheet name
* @param {string} queryString = Query
* @param {string} startRow = startRow
* @param {string} startColumn = startColumn
* @param {string} mergeYn = ์
๋ณํฉ์ด ๋์ด์์ผ๋ฉด(Y) delete Rows, ์
๋ณํฉ์ด ๋์ด์์ง ์์ผ๋ฉด(N) clear Rows
* @param {string} mergeArea = ์
๋ณํฉํ ์์ญ - ์์ ์ฒซ๋ฒ์งธ, ์ธ๋ฒ์งธ ์
์ ๋ณํฉ ์ "[1, 3]"
* @param {string} headerYn = ํค๋(์ปฌ๋ผ์ ๋ณด) ๋ณด์ฌ์ค์ง ์ฌ๋ถ
*/
function queryAndBinding(sheetUrl, sheetName, queryString, startRow, startColumn, mergeYn, mergeArea, headerYn) {
var request = {
query : queryString,
useLegacySql : false
};
var queryResults = BigQuery.Jobs.query(request, GLOBAL_PROJECT_ID);
var jobId = queryResults.jobReference.jobId;
// Check on status of the Query Job.
var sleepTimeMs = 10;
while ( !queryResults.jobComplete ) {
Utilities.sleep(sleepTimeMs);
sleepTimeMs *= 2;
queryResults = BigQuery.Jobs.getQueryResults(GLOBAL_PROJECT_ID, jobId);
}
// Get all the rows of results.
var rows = queryResults.rows;
while (queryResults.pageToken) {
queryResults = BigQuery.Jobs.getQueryResults(GLOBAL_PROJECT_ID, jobId, {
pageToken: queryResults.pageToken
});
rows = rows.concat(queryResults.rows);
}
var sheet = SpreadsheetApp.openByUrl(sheetUrl).getSheetByName(sheetName);
if (rows) {
// Append the headers.
var headers = queryResults.schema.fields.map(function(field) {
return field.name;
});
if ( !isNull(headerYn) && headerYn == "Y" ) {
console.log("show headers");
sheet.appendRow(headers);
}
// Append the results.
var data = new Array(rows.length);
for (var i=0; i<rows.length; i++) {
var cols = rows[i].f;
data[i] = new Array(cols.length);
for (var j=0; j<cols.length; j++) {
data[i][j] = cols[j].v;
}
}
sheet.getRange(startRow, startColumn, rows.length, headers.length).setValues(data);
var rangeObj = new Object();
rangeObj.startRow = startRow;
rangeObj.startColumn = startColumn;
rangeObj.endRow = rows.length;
rangeObj.endColumn = headers.length;
if ( !isNull(mergeArea) ) {
mergeCell(sheetUrl, sheetName, mergeArea);
}
} else {
alertMessage("โ ๏ธ Data Load Failed!", "์กด์ฌํ๋ ๋ฐ์ดํฐ๊ฐ ์์ต๋๋ค. \\r\\n");
Logger.log('No rows returned.');
return null;
}
return rangeObj;
}
/**
* ๋จ์ผ cell ๋ฐ์ดํฐ ์ถ์ถ
* @param {obj} sheet = ์์
ํ sheet
* @param {string} cellId = ์์
ํ cellId
*/
function getCellDataBySheet(sheet, cellId) {
try {
const sheet = SpreadsheetApp.openByUrl(sheetUrl).getSheetByName(sheetName);
const val = sheet.getRange(cellId).getValue();
if ( isNull(val == 0 ? "0" : val) ) {
return false;
}
return val;
} catch (error) {
const msg01 = error.message;
alertMessage("โ Error", "(getCellData) Error Message : " + msg01);
Logger.log(msg01);
}
}
/**
* Null ์ฒดํฌ
* @param {obj} obj = Null ์ฒดํฌ ๋์
*/
function isNull(obj) {
return (typeof obj != "undefined" && obj != null && obj != "") ? false : true;
}
/**
* ํ
๋๋ฆฌ ์ค์ - ๊ฐ์ ๊ฐ์ ธ์์ ๊ฐ์ ๊ฐ์ ธ์จ ์์ญ์ border ์ค์
* @param {obj} sheet = ์์
ํ sheet
* @param {string} sheetName = ์์
ํ sheet name
* @param {obj} dataRange = ์์
ํ sheet range
* @param {boolean} flag = set border status
*/
function setTableBorderBySheet(sheet, dataRange, flag) {
try {
sheet.getRange(dataRange.startRow, dataRange.startColumn, dataRange.endRow, dataRange.endColumn).setBorder(flag, flag, flag, flag, flag, flag);
} catch (error) {
const msg01 = error.message;
alertMessage("โ Error", "(setTableBorder) Error Message : " + msg01);
Logger.log(msg01);
}
}
/**
* Alert Message for Google sheet.
* @param {string} tit = ํ์ดํ
* @param {string} msg = ๋ฉ์ธ์ง
*/
function alertMessage(tit, msg) {
const ui = SpreadsheetApp.getUi();
ui.alert(tit, msg, ui.ButtonSet.OK);
}
/**
* ๊ฒ์์กฐ๊ฑด ๋๋ฝ
* @param {string} param = parameter
*/
function alertMissParam(param) {
const ui = SpreadsheetApp.getUi();
ui.alert("โ Error", param + " ์กฐ๊ฑด์ด ๋๋ฝ๋์์ต๋๋ค.", ui.ButtonSet.OK);
}