Dynamic Office Script date for modified row

1.5k Views Asked by At

I am trying to get the date a row was modified inserted into a Modified Date column for that particular row using Office Scripts for Excel Online. This is the closest I can get since I haven't found scripts that that get just the modified row which will need to be dynamic. Details are in the comments in the code below:

function main(workbook: ExcelScript.Workbook) {
// This finds the location of the cell at K2 (Modified Date column is on K:K) but actually 
//want to get the modified row location.
 let datetimeRange = workbook.getWorksheet("Form1").getRange("K2");

// Get dynamic range of the worksheet. I'm not sure if this will work since there might be 
//some blanks. The number of rows and possibly columns will change so needs to be dynamic.
let myRange = 
workbook.getWorksheet("Form1").getRange("A1").getSurroundingRegion().getAddress();

// Get the current date and time with the JavaScript Date object.
let date = new Date(Date.now());

// This adds the date string to K2 but actually only want it added to Modified Date in the 
//Modified Date column (dynamic since columns can change) for only the row that was modified.
datetimeRange.setValue(date.toLocaleDateString() + ' ' + date.toLocaleTimeString());
 }

enter image description here

1

There are 1 best solutions below

0
On

I know this is an old thread, but for the others that may come here looking for a solution, this one is macro-less:

  1. Turn on iterative calculations for formulas (Options -> Formulas).
  2. Add this formula in K2: =IF(CELL("row")=ROW(K2), NOW(), K2)
  3. Drag that down your K column and you're done!