GCP/Apps Script

Apps Script๋กœ Google Sheets์˜ Dropdown ์ƒ์„ฑํ•˜๊ธฐ

whistory 2022. 9. 6. 08:02
๋ฐ˜์‘ํ˜•

 

๐Ÿ’ก 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);
}
๋ฐ˜์‘ํ˜•