GCP/Apps Script

Apps Script๋กœ Google Sheets์˜ ํ™œ์„ฑ(์„ ํƒ)์˜์—ญ ์ง€์ •

whistory 2023. 3. 8. 16:36
๋ฐ˜์‘ํ˜•

๐Ÿ’ก Apps Script๋กœ Google Sheets์— ํ™œ์„ฑ(์„ ํƒ)์˜์—ญ์„ ์ง€์ •ํ•œ๋‹ค.
     ์›น ํ”„๋กœ๊ทธ๋žจ์—์„œ setFocus()์™€ ๋น„์Šทํ•œ ํšจ๊ณผ๋ฅผ ์ค„ ์ˆ˜ ์žˆ๋‹ค.
     ๊ฐ„๋‹จํ•œ ์‚ฌ์šฉ๋ฒ•๊ณผ ์‘์šฉ ์˜ˆ์ œ๋ฅผ ์•Œ์•„๋ณธ๋‹ค.

 

SpreadsheetApp ์—์„œ ์ œ๊ณตํ•˜๋Š”

setActiveRange() ๋ฅผ ์ด์šฉํ•ด ์•„๋ž˜์™€ ๊ฐ™์ด Google Sheets์˜ ํ™œ์„ฑ(์„ ํƒ)์˜์—ญ์„ ์ง€์ •ํ•ด ์ค„ ์ˆ˜ ์žˆ๋‹ค.

 

์•„๋ž˜์™€ ๊ฐ™์ด setActiveRange() ์•ˆ์— range ๋ฅผ ๋„ฃ์–ด์ค˜์•ผ ํ•œ๋‹ค.

// ๋ฐฉ์‹ #1
const range = sheet.getRange("A1");
sheet.setActiveRange(range);

// ๋ฐฉ์‹ #2
sheet.setActiveRange(sheet.getRange("A1"));

๋‹จ์ผ ์˜์—ญ ์ง€์ •

function setCellPositon() {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sheet = ss.getSheetByName("์‹œํŠธ22");

  const range = sheet.getRange("A1");
  
  sheet.setActiveRange(range);
}

 

๋‹ค์ค‘ ์˜์—ญ ์ง€์ •

function setCellPositon() {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sheet = ss.getSheetByName("์‹œํŠธ22");

  const range = sheet.getRange("A1:F"+sheet.getLastRow());
  
  sheet.setActiveRange(range);
}

 

 

 

 

์‘์šฉํ•ด๋ณด๊ธฐ

Apps Script๋กœ Google Sheets์˜ ์ €์žฅ ์˜์—ญ ํ•„์ˆ˜ ๊ฐ’ ์ฒดํฌํ•˜๊ธฐ

 

Apps Script๋กœ Google Sheets์˜ ์ €์žฅ ์˜์—ญ ํ•„์ˆ˜ ๊ฐ’ ์ฒดํฌํ•˜๊ธฐ

Sheet ๋ฐ์ดํ„ฐ๋ฅผ BigQuery์— ์ €์žฅํ•  ๋•Œ, ๋‚˜์ด, ์„ฑ๋ณ„์„ ํ•„์ˆ˜ ๊ฐ’์œผ๋กœ ์ž…๋ ฅํ•˜๊ณ  ์‹ถ๋‹ค. dataNullCheck ํ•จ์ˆ˜์™€, reqIdx ๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค. function insertSheetData() { const sheetUrl = SpreadsheetApp.getActiveSpreadsheet().getUrl(); const shee

whiseung.tistory.com

ํ•„์ˆ˜ ๊ฐ’์„ ์ฒดํฌํ•˜๊ณ , ํ•„์ˆ˜ ์ž…๋ ฅ์—์„œ ๊ฑธ๋ฆฐ ์…€๋กœ ํฌ์ธํŠธ๋ฅผ ์ด๋™ํ•ด๋ณธ๋‹ค.

 

์„ฑ๋ณ„(C5)๋Š” ํ•„์ˆ˜์ž…๋ ฅ ๊ฐ’์ž…๋‹ˆ๋‹ค.

 

๋ผ๋Š” ๋ฉ”์‹œ์ง€์—์„œ ์…€ ๊ฐ’์ธ ‘C5’๋งŒ ์ถ”์ถœํ•œ๋‹ค.

์ •๊ทœ์‹์„ ์ด์šฉํ•ด ๊ด„ํ˜ธ ์•ˆ์— ๊ฐ’์„ ์ถ”์ถœํ•œ๋‹ค.

const regExp = /\\(([^)]+)\\)/;
const matches = regExp.exec(validationCheck);
console.log(matches[1]);
console.log(validationCheck + "๋Š” ํ•„์ˆ˜์ž…๋ ฅ ๊ฐ’์ž…๋‹ˆ๋‹ค.")

 

 

์ด์ œ ํ•„์ˆ˜ ๊ฐ’์„ ์ฒดํฌํ•˜๋Š” ๋ถ€๋ถ„์— ์•„๋ž˜ ๋กœ์ง์„ ์ถ”๊ฐ€ํ•œ๋‹ค.

const regExp = /\\(([^)]+)\\)/;
const matches = regExp.exec(validationCheck);

const range = sheet.getRange(matches[1]);
sheet.setActiveRange(range);
function insertSheetData() {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sheet = ss.getSheetByName("Sheet3");
  const table_id  = "insert_test";

  const reqIdx =  [
                      {name:'์ด๋ฆ„(A' , index:1}
                    , {name:'๋‚˜์ด(B' , index:2}
                    , {name:'์„ฑ๋ณ„(C' , index:3}
                  ];

  const validationCheck = dataNullCheck(sheet, 2, reqIdx);
  if ( validationCheck != "done" ) {
    const regExp = /\\(([^)]+)\\)/;
    const matches = regExp.exec(validationCheck);

    const range = sheet.getRange(matches[1]);
    sheet.setActiveRange(range);

    console.log(validationCheck + "๋Š” ํ•„์ˆ˜์ž…๋ ฅ ๊ฐ’์ž…๋‹ˆ๋‹ค.")
    return;
  }

  const colInfo = {startRow:2, startColumn:1};

  const saveTime = getDateTime("BQ");
  const addValues = {
                      status      : "Y"
                      , saveTime  : saveTime
                  };

  const resultFlag = loadSpreadsheet(sheet, global_datasetId, table_id, colInfo, addValues);

  console.log(`Result Flag : ${resultFlag}`);
}

/**
 * ๋ฐ์ดํ„ฐ null ์ฒดํฌ. like validation check
 * @param {object} spreadSheet  = ํ•ด๋‹น function์„ ํ˜ธ์ถœํ•œ spreadsheet
 * @param {string} startRow     = ์‹œ์ž‘ ํ–‰๋ฒˆํ˜ธ
 * @param {object} keyIdx       = ํ‚ค๊ฐ’ ์ •๋ณด
 */
function dataNullCheck (spreadSheet, startRow, keyIdx) {
  for ( q in keyIdx ) {
    var endRow = spreadSheet.getLastRow()-startRow+1;
    var data = spreadSheet.getSheetValues(startRow, keyIdx[q].index, endRow, 1);

    for ( i in data ) {
      var value = data[i];
      if ( isNull(value) ) {
        return keyIdx[q].name + (parseInt(i)+parseInt(startRow)) + ")";
      }
    }
  }

  return "done";
}

 

 

 

๋ฐ˜์‘ํ˜•