Excel Online Script that uses date from cell

68 views Asked by At

I am trying to make a script in Excel online. I need to paste the date in F1, like 13/2/2023, and then G1 should be 31/12/2023, so the last year of the same date. The date should be taken from a cell, A13, which looks like this: "Applied filters:PCA Costing Year Plan is 21291544 2022 1872", where 2022 is the year that should be used, so in this case it should be any date in 2022 in F1 and 31/12/2022 in G1.

Here is what I wrote so far but it does not work:

function main(workbook: ExcelScript.Workbook) {
  let selectedSheet = workbook.getActiveWorksheet();
  // Get the year from cell A13
  const yearString = /(\d{4})/.exec(selectedSheet.getCell("A13").getText())[1];
  const year = parseInt(yearString);

  // Set the date in cell F1
  const date = new Date(year, 1, 13);
  selectedSheet.getCell("F1").setValue(date);

  // Set the last day of the year in cell G1
  const lastDayOfYear = new Date(year, 11, 31);
  selectedSheet.getCell("G1").setValue(lastDayOfYear);
}
1

There are 1 answers

0
Rosenz5 On BEST ANSWER

I solved it with this code:

selectedSheet.getRange("G1").setValue(new Date(new Date().getFullYear(), 11, 
31).toLocaleDateString());