使用单元格日期的 Excel Online 脚本

问题描述 投票:0回答:1

我正在尝试在 Excel 中在线制作一个脚本。 我需要将日期粘贴到 F1 中,例如 13/2/2023,然后 G1 应该是 31/12/2023,所以同一日期的最后一年。 该日期应取自单元格 A13,如下所示:“应用的过滤器:PCA 成本计算年度计划为 21291544 2022 1872”,其中 2022 是应使用的年份,因此在本例中它应该是F1 为 2022 年,G1 为 2022 年 12 月 31 日。

这是我到目前为止所写的,但它不起作用:

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);
}
typescript excel-online
1个回答
0
投票

我用这段代码解决了它:

selectedSheet.getRange("G1").setValue(new Date(new Date().getFullYear(), 11, 
31).toLocaleDateString());
© www.soinside.com 2019 - 2024. All rights reserved.