Home > front end >  Google Apps Script: How to find column number of specific value in array?
Google Apps Script: How to find column number of specific value in array?

Time:10-07

What I'm trying to achieve is to get the column number of a specific cell. If the cell value in the array equals to the value of today's date, then give back the column number of that cell.

dateArrayD = 1 row containing all the dates from January 1 to December 31, formatted

dateTodayD = the date today, formatted

So if dateTodayD equals dateArrayD, then dateCol would be the column number. I simply can't find a solution for this.

for(var z = 0; z < dateArrayD.length; z  ){
    if(dateTodayD !== dateArrayD[0][z]){
      var dateCol = z;
    } // end if
  } // for end

CodePudding user response:

This function adds a line feed and row , column to each cell.

function calcRowColumn() {
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getSheetByName('Sheet0');
  const sr = 4;//data start row
  const sc = 4;//data start column
  const rg = sh.getRange(sr,sc,sh.getLastRow() - sr   1, sh.getLastColumn() - sc   1);
  const vs = rg.getDisplayValues();
  vs.forEach((r,i) => {
    let row = i   sr;
    r.forEach((c,j) => {
      let col = j   sc;
      vs[i][j]  = `\n${row},${col}`;
      
    })
  })
  rg.setValues(vs);
}

Start Data:

enter image description here

End Data:

enter image description here

  • Related