GCP/Apps Script

Apps Script๋กœ Google Sheets์˜ Filter ์„ค์ •ํ•˜๊ธฐ

whistory 2023. 3. 2. 15:23
๋ฐ˜์‘ํ˜•

๐Ÿ’ก Apps Script๋กœ Google Sheets์—์„œ ํ•„ํ„ฐ๋ฅผ ์ƒ์„ฑํ•˜๊ณ , ์กฐ๊ฑด์„ ์ ์šฉํ•ด ๋ณธ๋‹ค.

      ๋‚˜์•„๊ฐ€ ํ•„ํ„ฐ ์กฐ๊ฑด์ด ์ ์šฉ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ์ƒˆ๋กœ์šด ์‹œํŠธ๋กœ ์ถ”์ถœํ•ด๋ณธ๋‹ค.

 

 

์•„๋ž˜์™€ ๊ฐ™์€ ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ๋‹ค.

 

 

ํ•„ํ„ฐ ์ƒ์„ฑํ•˜๊ธฐ

function createFilters() {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sheet = ss.getSheetByName("tempature_all");
  const range = sheet.getRange("A:E");
  range.createFilter();
}

ํ•„ํ„ฐ ์‚ญ์ œํ•˜๊ธฐ

function createFilters() {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sheet = ss.getSheetByName("tempature_all");
  const range = sheet.getRange("A:E");
  const getFilter = range.getFilter();
  getFilter.remove();
}

 

 

 

 

ํ•„ํ„ฐ๋กœ ์ผ์น˜ํ•˜๋Š” ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ

country ๊ฐ€ ‘KR’์ธ ๋ฐ์ดํ„ฐ๋งŒ ์ถ”์ถœํ•œ๋‹ค.

function createFilters() {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sheet = ss.getSheetByName("tempature_all");
  const range = sheet.getRange("A:E");
  const getFilter = range.getFilter();
  
  if ( getFilter == null ) {
    const newFilter = range.createFilter();

    const coll1 = 1;
    const Filter_Criteria1 = SpreadsheetApp.newFilterCriteria().whenTextContains(["KR"]);
    newFilter.setColumnFilterCriteria(coll1,Filter_Criteria1);
    
  } else {
    getFilter.remove();
  }
}

 

 

์ค‘๋ณต ํ•„ํ„ฐ ์ ์šฉ

country ๊ฐ€ ‘KR’์ธ ๋ฐ์ดํ„ฐ๋งŒ ์ถ”์ถœํ•œ๋‹ค.

temperature_air_avg_avg ๊ฐ€ 50์„ ์ดˆ๊ณผํ•˜๋Š” ๋ฐ์ดํ„ฐ๋งŒ ์ถ”์ถœํ•œ๋‹ค.

function createFilters() {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sheet = ss.getSheetByName("tempature_all");
  const range = sheet.getRange("A:E");
  const getFilter = range.getFilter();
  
  if ( getFilter == null ) {
    const newFilter = range.createFilter();

    const coll1 = 1;
    const Filter_Criteria1 = SpreadsheetApp.newFilterCriteria().whenTextContains(["KR"]);
    newFilter.setColumnFilterCriteria(coll1,Filter_Criteria1);

    const coll3 = 3;
    const Filter_Criteria3 = SpreadsheetApp.newFilterCriteria().whenNumberGreaterThan(50);
    newFilter.setColumnFilterCriteria(coll3,Filter_Criteria3);

  } else {
    getFilter.remove();
  }
}

 

์ถ”์ถœํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ์ƒˆ๋กœ์šด sheet ๋กœ ์ €์žฅํ•œ๋‹ค.

ํ•„ํ„ฐ๋กœ ์ถ”์ถœ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ์ƒˆ๋กœ์šด sheet์— ์ €์žฅํ•˜๊ณ ,

์ƒ์„ฑํ•œ ํ•„ํ„ฐ๋Š” ์ œ๊ฑฐํ•œ๋‹ค.

function createFilters() {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sheet = ss.getSheetByName("tempature_all");
  const range = sheet.getRange("A:E");
  const getFilter = range.getFilter();
  if ( getFilter == null ) {
    const newFilter = range.createFilter();

    const coll1 = 1;
    const Filter_Criteria1 = SpreadsheetApp.newFilterCriteria().whenTextContains(["KR"]);
    newFilter.setColumnFilterCriteria(coll1,Filter_Criteria1);

    const coll3 = 3;
    const Filter_Criteria3 = SpreadsheetApp.newFilterCriteria().whenNumberGreaterThan(50);
    newFilter.setColumnFilterCriteria(coll3,Filter_Criteria3);

    const newSheet = ss.insertSheet();
    newSheet.setName("KR_Data");
    range.copyTo(newSheet.getRange(1,1));

    newFilter.remove();

  } else {
    getFilter.remove();
  }
}

 

 

 

 

์ด ์ž‘์—…์€

๋ฐ์ดํ„ฐ๋ฅผ ์กฐํšŒ ํ•˜๊ณ , ํŠน์ •์กฐ๊ฑด์„ ์ง€์†์ ์œผ๋กœ ์ถ”์ถœํ•ด์•ผ ํ• ๋•Œ

์‚ฌ์šฉํ•˜๋ฉด ์ข‹์„๊ฒƒ ๊ฐ™๋‹ค.

๋ฐ˜์‘ํ˜•