๋ฐ์ํ
๐ก Apps Script๋ก Google Sheets์์ Dropdown(Data Validation)์ ์์ฑํ๋ ๋ฐฉ๋ฒ์ ์์๋ณธ๋ค.
Google Sheets์์ ๋ฐ์ดํฐ๋ฅผ ์ ๋ ฅํ๊ฑฐ๋ ์กฐํ ํ ๋,
Dropdown ์ผ๋ก ์ ๋ ฅ ํ ๊ฒฝ์ฐ๊ฐ ์๊ธด๋ค.
1. Google Sheets ๋ฐ์ดํฐ ์์ญ์ Dropdown(Data validation) ์ผ๋ก ์์ฑ
function settingDropdown() {
const sheet = SpreadsheetApp.getActive().getSheetByName("Sheet1");
// Dropdown ๋ฐ์ดํฐ ์์ญ ์ค์
const validationCell = sheet.getRange("C2:C12");
// ์์ฑํ ์์ญ ์ค์
const setCell = sheet.getRange("E2");
// ์์ฑํ ์์ญ ์ด๊ธฐํ
setCell.clearContent().clearDataValidations();
// validation rule ์์ฑ
const validationRule = SpreadsheetApp.newDataValidation()
.requireValueInRange(validationCell)
.build();
// ์์ฑํ ์์ญ์ rule ํ ๋น
setCell.setDataValidation(validationRule);
}
2. BigQuery ๋ฐ์ดํฐ๋ฅผ Dropdown(Data validation) ์ผ๋ก ์์ฑ
function settingDropdownFromBigQuery () {
const sheet = SpreadsheetApp.getActive().getSheetByName("Sheet1");
// Dropdown ๋ฐ์ดํฐ ์์ญ ์ค์
const setCell = sheet.getRange("F2");
// ์์ฑํ ์์ญ ์ด๊ธฐํ
setCell.clearContent().clearDataValidations();
// Query ์์ฑ
var datasetId = global_datasetId;
var tableId = "standard_long";
var queryString = 'SELECT BillingDocumentItemText FROM `'
+ GLOBAL_PROJECT_ID + "." + datasetId + "." + tableId
+ '` WHERE TransactionCurrency = "KRW" AND BillingDocumentItem = "82" ;'
// Query ํธ์ถ
var result = queryExcute(queryString);
// ๋ฐฐ์ด์ ๋ด๊ธฐ
const arr = Array.from({length: result.length}, (undefined, i) => result[i].f[0].v );
// validation rule ์์ฑ
var rule = SpreadsheetApp.newDataValidation().requireValueInList(arr).build();
// ์์ฑํ ์์ญ์ rule ํ ๋น
setCell.setDataValidation(rule);
// ๋งจ์ ์ฒซ๋ฒ์งธ ๊ฐ ์ธํ
setCell.setValue(arr[0]);
}
3. ๋ ๋ / ์ dropdown ์์ฑํ๊ธฐ
/**
* ๋
๋ ์
ํ
* @param {string} sheetUrl = ํด๋น function์ ํธ์ถํ sheet์ URL
*/
function settingYearDropDownList(sheetUrl) {
const thisYear = common.getDateTime().substr(0,4);
var arr = new Array();
for ( var i=thisYear; i>=2019; i-- ) {
arr.push(i);
}
const rule = SpreadsheetApp.newDataValidation().requireValueInList(arr)
.setAllowInvalid(false)
.build();
const sheet = SpreadsheetApp.getActive().getSheetByName("Sheet1");
sheet.getRange("C4").setDataValidation(rule);
sheet.getRange("C4").setValue(thisYear);
}
/**
* ์ ์
ํ
* @param {string} sheetUrl = ํด๋น function์ ํธ์ถํ sheet์ URL
*/
function settingMonthDropDownList(sheetUrl) {
const thisMonth = common.getDateTime().substr(4,2);
const arr = Array.from({length: 12}, (undefined, i) => i+1);
const rule = SpreadsheetApp.newDataValidation().requireValueInList(arr)
.setAllowInvalid(false)
.build();
const sheet = SpreadsheetApp.getActive().getSheetByName("Sheet1");
sheet.getRange("D4").setDataValidation(rule);
sheet.getRange("D4").setValue(thisMonth);
}
๋ฐ์ํ
'GCP > Apps Script' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Apps Script๋ก Google Sheets์ ์์ ์ ์ฉํ๊ธฐ (0) | 2022.09.06 |
---|---|
Apps Script๋ก Google Sheets์ ์กฐ๊ฑด๋ถ์์ ์ค์ ํ๊ธฐ (0) | 2022.09.06 |
Apps Script๋ก BigQuery ๋ฐ์ดํฐ๋ฅผ Google Sheets์ ๋ฟ๋ ค์ฃผ๊ธฐ (0) | 2022.09.06 |
Apps Script๋ก BigQuery ์ฐ๊ฒฐ (0) | 2022.09.06 |
Apps Script์์ ๋ก๊ทธ์ธํ ์ฌ์ฉ์ ์ ๋ณด ๊ฐ์ ธ์ค๊ธฐ (0) | 2022.08.26 |