GCP/Apps Script

Apps Script๋กœ Google Sheets์˜ Number Format ์„ค์ •ํ•˜๊ธฐ + Date Format

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

๐Ÿ’กApps Script๋กœ Google Sheets์˜ ์ˆซ์ž์„œ์‹, ๋‚ ์งœ์„œ์‹์„ ์ ์šฉํ•˜๋Š”๋ฒ•์„ ์•Œ์•„๋ณธ๋‹ค.

 

๋ญ”๊ฐ€ ํ—ˆ์ „ํ•˜๋‹ค.

 

์ˆซ์ž์ธ๋ฐ ์ฒœ๋‹จ์œ„ ์ฝค๋งˆ๊ฐ€ ์—†๋‹ค.

Google Sheets์—์„œ ์ œ๊ณตํ•˜๋Š” ์ˆซ์ž์„œ์‹์„ ์ ์šฉํ•˜๋ ค๊ณ  ํ•œ๋‹ค.

 

์ˆซ์ž์„œ์‹ ์ ์šฉ์€ ๊ฐ„๋‹จํ•˜๋‹ค.

Class Range | Apps Script | Google Developers

 

Class Range  |  Apps Script  |  Google Developers

์ด ํŽ˜์ด์ง€๋Š” Cloud Translation API๋ฅผ ํ†ตํ•ด ๋ฒˆ์—ญ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. Switch to English ์˜๊ฒฌ ๋ณด๋‚ด๊ธฐ Class Range ๋ฒ”์œ„ ์Šคํ”„๋ ˆ๋“œ์‹œํŠธ ๋ฒ”์œ„์— ์•ก์„ธ์Šคํ•˜๊ณ  ์ˆ˜์ •ํ•ฉ๋‹ˆ๋‹ค. ๋ฒ”์œ„๋Š” ์‹œํŠธ์˜ ๋‹จ์ผ ์…€ ๋˜๋Š” ์‹œํŠธ์˜ ์ธ์ ‘ ์…€ ๊ทธ๋ฃน์ผ

developers.google.com

 

 

 

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

var cell = sheet.getRange("B2");
// Always show 3 decimal points
cell.setNumberFormat("0.000");

 

 

 

์ž‘์„ฑ ์ค‘์ธ ์ฝ”๋“œ์— ๋ฐ˜์˜ํ•ด๋ณธ๋‹ค.

์˜์—ญ์— ์žˆ์–ด, ์ปฌ๋Ÿผ์€ ํ•ญ์ƒ ๋™์ผํ•  ๊ฒƒ์ด๊ณ ,

row count๋งŒ ๋‹ฌ๋ผ์งˆ ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์—

sheet.getLastRow() ๋ฅผ ์ด์šฉํ•ด ๋ฒ”์œ„๋ฅผ ์ง€์ •ํ•œ๋‹ค.

 

 

 

 

 

 

 

 

๊ทธ๋ฆฌ๊ณ  ์šฐ์ธก ์ •๋ ฌ์ด ์•ˆ๋˜๋Š” ๊ฒฝ์šฐ๋„ ์žˆ๊ธฐ ๋•Œ๋ฌธ์—,

sheet.setHorizontalAlignment("right"); ๋„ ์ถ”๊ฐ€ํ•˜์—ฌ ์šฐ์ธก ์ •๋ ฌ์„ ์ ์šฉํ•ด์ค€๋‹ค.

/**
 * BigQuery์—์„œ ๊ฐ€์ ธ์˜จ ๋ฐ์ดํ„ฐ๋ฅผ Sheets์— ๋ฟŒ๋ ค์ฃผ๊ธฐ
 */
function getDataFromBigquery() {
  const datasetId = global_datasetId;
  const tableId = "standard_long";
  const columns = 'BillingDocument, BillingQuantityUnit, TransactionCurrency, NetAmount, TaxAmount, "" AS NULLTXT, BillingDocumentItemText'
  const queryString = 'SELECT  ' + columns + ' FROM `'
              +  GLOBAL_PROJECT_ID + "." + datasetId + "." + tableId + '` WHERE BillingDocumentItem = "82" ORDER BY BillingDocument ASC ;';

  const sheetUrl = SpreadsheetApp.getActiveSpreadsheet().getUrl();
  const sheetName = "Sheet2"
  const dataRange = queryAndBinding(sheetUrl, sheetName, queryString, 2, 1, "N", null, "Y");
  
  const sheet = SpreadsheetApp.openByUrl(sheetUrl).getSheetByName(sheetName);
  if ( dataRange != null ) {
    // ์กฐ๊ฑด๋ถ€์„œ์‹ ์ œ๊ฑฐ
    sheet.clearConditionalFormatRules();
    // ํ…Œ๋‘๋ฆฌ ์„ค์ •
    setTableBorder(sheet, dataRange, true);

    // ์…€์ˆ˜์‹ ์ ์šฉ
    var calculateCell = sheet.getRange("F2:f"+sheet.getLastRow());
    calculateCell.setFormulaR1C1("=R[0]C[-2]-R[0]C[-1]");

    // ์ˆซ์ž์„œ์‹ ์ ์šฉ
    var applyRange = "D2:"+"F"+sheet.getLastRow();
    sheet.getRange(applyRange).setNumberFormat("#,##0").setHorizontalAlignment("right");

    // ์กฐ๊ฑด๋ถ€์„œ์‹ ์ ์šฉ - ์ผ์น˜ํ•˜๋Š” ํ…์ŠคํŠธ ๊ฐ•์กฐ
    setConditionalFormatCompareText(sheet, "KRW");
    // ์กฐ๊ฑด๋ถ€์„œ์‹ ์ ์šฉ - ํŠน์ •์ˆ˜์น˜ ์ด์ƒ ๊ฐ’ ๊ฐ•์กฐ
    setConditionalFormatCompareNumber(sheet, 200000);
  }
}

๊ฒฐ๊ณผ

 

 

 

 

 

 

 

 

 

 

 

์ˆซ์žํฌ๋งท ๋ง๊ณ ๋„, yyyy-mm-dd ๊ฐ™์€ ๋‚ ์งœํ˜•์‹ ์ง€์ •๋„ setNumberFormat()๋ฅผ ์ด์šฉํ•ด ์„ค์ •ํ• ์ˆ˜์žˆ๋‹ค.

activeCell.setValue(oldValue).setNumberFormat('yyyy-mm-dd');

Date and Number Formats | Sheets API | Google Developers

 

Date and Number Formats  |  Sheets API  |  Google Developers

Send feedback Date and Number Formats Date-time and number formats let you control how your data appears in a sheet. Google Sheets provides some common formats to choose from, but you can also define your own formats. In the Sheets UI, you apply number and

developers.google.com

 

๋ฐ˜์‘ํ˜•