Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Set cell background of cell after value is appended

I have a script that is appending the value in cell D2 then adding one to that cell, basically stacking a list of numbers. To make it more user friendly, I want to activate the latest cell (which works), but I also want to set the background color to cyan for 2 seconds so the user knows exactly where to go.

I have it close, but instead of seeing the cyan color, I’m just seeing the row activate.

Any thoughts?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

function copynewentry() {

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var idstoadd = ss.getSheetByName("Resources");
  var fulllist = ss.getSheetByName("Daily");
  var tag = idstoadd.getRange('D2');

  Logger.log("Option#1 - Start")

  // step 1 - get the last row on the target: Full List
  var last_row = fulllist.getRange(1, 1, fulllist.getLastRow(), 1).getValues().filter(String).length;
  Logger.log("Step#1 - the last row on fulllist = "+last_row)

  // step 2 - create a target range (or at least the top left corner of the target)
  var targetrange = fulllist.getRange(last_row+1,1);
  Logger.log("Step#2 - the target range will be "+targetrange.getA1Notation())

  // step 3 - create a source range
  // how many header rows
  var numheaderrows = 0
  // so the range will be...
  // var sourcerange = idstoadd.getRange(1+numheaderrows, 1, idstoadd.getLastRow()-numheaderrows, 1);
  var sourcerange = idstoadd.getRange('D2');
  Logger.log("Stewp#3 - the sourcerange = "+sourcerange.getA1Notation());
  
  // step4 - copy from source to target
  // method = range.copyto
  // so copyto from the source range to the target range
  sourcerange.copyTo(targetrange,{contentsOnly:true});
  Logger.log("Step#4 - copied the source range to the target range and flushed");
  
  
tag.setValue(tag.getValue() + 1);


lastRowOfCol(1); //Enter the column number you want to use as the base of the search


  

  // step 6 - apply all the pending changes.
  SpreadsheetApp.flush();
  Logger.log("Step#6 - Flushed the spreadsheet")
  Logger.log("Option#1 - Completed")
  return false;
}

function lastRowOfCol(column) {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();
  var total = sheet.getMaxRows();
  var values = sheet.getRange(1, column, total).getValues();
  for (var i = total - 1; values[i] == "" && i > 0; i--) {}
  var last = sheet.getRange(i + 1, column);
  var range = sheet.getRange(i + 1, 1, 1, sheet.getLastColumn());
  sheet.setActiveRange(range);

  const bgColor = last.getBackground();
  last.setBackground('cyan');
  Utilities.sleep(2000);
  last.setBackground(bgColor);
}

>Solution :

In your script, how about the following modification?

From:

last.setBackground('cyan');
Utilities.sleep(2000);

To:

last.setBackground('cyan');
SpreadsheetApp.flush(); // Added
Utilities.sleep(2000);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading