Home > database >  Looping over a range and saving the a1 notation and cell value based on condition in a deictionary
Looping over a range and saving the a1 notation and cell value based on condition in a deictionary

Time:11-29

I want to get the range and its value stored in a dictionary when a condition is met-

I have a range as below,

I want to store the A1 notation and value where ever in the column starting from D5 we get a concatenated string with length 3

enter image description here

I tried with this code

concat_rows = {};
  p_rows = P_details.getRange("D5:D").getValues();
  for (var h in p_rows){
    if(p_rows[h][0].split('-').length== 3){
      // code to push the cell value as key and A1 notation as value in concat_rows
      
    }
  }

The dictionary concat_rows should be

{ a-b-c : D5, d-e-f : D11} 

Please help @Martin

CodePudding user response:

You should easily adapt the function I wrote to your case

function getA1NotationOfCells() {
  let sheet = SpreadsheetApp.openById(REPLACE).getSheetByName(REPLACE);
  let dict = {}
  const COLUMN = 'D'
  const START_LINE = 5
  const LAST_ROW = sheet.getLastRow();
  
  for(let i = START_LINE; i <= LAST_ROW; i  ) {
    let cellValue = sheet.getRange(COLUMN i).getValue()
    if(cellValue && cellValue.split('-').length === 3) {
      dict[cellValue] = COLUMN i
    }
  }

  return dict
}

Note that you can get rid of START_LINE and start at 1 if you want.

  • Related