Home > Back-end >  Overwrite existing text using IF statement against dates and variable column headers reference
Overwrite existing text using IF statement against dates and variable column headers reference

Time:11-24

trying to get some help on overwriting some existing data using scripts, and using dynamic column headers. Appreciate the help as I think this is above my experience level at the moment.

Desired outcome:

I would like to change (as in the example below) any cells in column A that are currently set to "Before" to say "AFTER" (in all caps) if Date 1 is after Date 3, and highlight them. I also do not want to change any data in column A if they do not meet this criteria, which means if a cell in column A already says "After" it can remain as is.

I think I can figure out the highlight coding portion, but it's the first part I'm getting stuck on.

My current issue is that I'm finding it difficult to reference column headers for this script, as I need to do this because the headers can be in different columns, so relying on a constant reference to a specific column is not possible. This is also making me confused on how to do the .setValues for this as well since it's going through the entire column potentially.

What I've tried so far:

  1. Attempted to utilize the code in reference 1 below to work for this, but it's overwriting all cells instead of just the cells that are affected
  2. Not sure if I should make functions within a function and use the createTextFinder to find these headers? This seems wrong because I can only use it once and I have 3 columns I need to find
  3. Tried to utilize some scripts that change columns to letters but I'm unsure how to utilize them properly

My Code: And yes, it doesn't work, but this is my best attempt to set it up:

function changeBefore() {

  const ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1');
  const tf = ss.createTextFinder('Before').matchEntireCell(true).matchCase(false);
  const beforeCell = tf.findNext()

  var date1Range = date1.getRange()
  var date3Range = date3.getRange()
  var bacRange = beforeorAfterColumn.getRange()

  if (bacRange=='Before'==date1Range>date3Range){
    beforeorAfterColumn.getRange().setValues('AFTER');

  }//end of if


  function beforeorAfterColumn(){
  var headers = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1').getDataRange().getValues().shift();
  var colindex = headers.indexOf('Before or After');
  return colindex 1

  function date1(){
  var headers = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1').getDataRange().getValues().shift();
  var colindex = headers.indexOf('Date 1');
  return colindex 1

  function date3(){
  var headers = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1').getDataRange().getValues().shift();
  var colindex = headers.indexOf('Date 3');
  return colindex 1
      }//end of date 3 function
    }//end of date1 function
  }//end of beforeorAfterColumn function
}//end of changeBefore function
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

Example of current sheet:

enter image description here

Desired outcome of script:

enter image description here

Resources/References:

  1. enter image description here

  • Related