简体   繁体   English

数组中的自定义日期格式值

[英]Custom Date format values in array

I want to format dates in a Table column in Excel 365 Web using a custom format.我想使用自定义格式在 Excel 365 Web 的表格列中格式化日期。 I can format a cell but not the cell value picked up in a variable or array.我可以格式化一个单元格,但不能格式化在变量或数组中拾取的单元格值。

 function main(workbook: ExcelScript.Workbook) { let selectedSheet = workbook.getActiveWorksheet(); // Set number format for range A2 on selectedSheet // selectedSheet.getRange("A2").setNumberFormatLocal("yyyy-mmm"); let v = selectedSheet.getRange("A2").getValue(); console.log(v); }

I want the dates to show up as yyyy-mmm .我希望日期显示为yyyy-mmm Can anyone show how to custom date format an array of values?谁能展示如何自定义日期格式的值数组?

If you use getTexts() it will return an array of the formatted text values.如果您使用 getTexts() ,它将返回格式化文本值的数组。 I updated the v variable in your example to have the range A2:A3 and changed the method from getValue() to getTexts():我在您的示例中更新了 v 变量,使其范围为 A2:A3,并将方法从 getValue() 更改为 getTexts():

function main(workbook: ExcelScript.Workbook) {
  let selectedSheet = workbook.getActiveWorksheet();
  // Set number format for range A2 on selectedSheet
  // selectedSheet.getRange("A2").setNumberFormatLocal("yyyy-mmm");
  let v = selectedSheet.getRange("A2:A3").getTexts();
  console.log(v);
}

My experience with Javascript and date formatting has never been a favourable one, not like Excel or any of the Microsoft formatting options across it's platforms you typically have access to.我对 Javascript 和日期格式的体验从来都不是一个好的体验,不像 Excel 或您通常可以访问的平台上的任何 Microsoft 格式选项。

When it comes to working with Javascript dates, all I could find was cobbling the format together by using a string based approach.在使用 Javascript 日期时,我所能找到的只是使用基于字符串的方法将格式拼凑在一起。

You first need to turn the Excel date into a JS date and then do the work from there.您首先需要将 Excel 日期转换为 JS 日期,然后从那里开始工作。

Try this...尝试这个...

function main(workbook: ExcelScript.Workbook)
{
  let selectedSheet = workbook.getActiveWorksheet();
  // Set number format for range A2 on selectedSheet
  // selectedSheet.getRange("A2").setNumberFormatLocal("yyyy-mmm");
  let v = selectedSheet.getRange("A2").getValue() as number;
  
  let javaScriptDate = new Date(Math.round((v - 25569) * 86400 * 1000));
  
  const months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
  let formattedDate = javaScriptDate.getFullYear() + '-' + months[javaScriptDate.getMonth()];

  console.log(formattedDate)
}

Microsot actually give you a small headstart with this documentation which I have applied to my answer... Microsot 实际上为您提供了一份我已应用于我的答案的文档...

https://docs.microsoft.com/en-us/office/dev/scripts/resources/samples/excel-samples#dates https://docs.microsoft.com/en-us/office/dev/scripts/resources/samples/excel-samples#dates

It's not ideal but (I believe) it's your best option.这并不理想,但(我相信)这是您的最佳选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM