使用 Power Automate 动态更改 Excel 工作表日期的 Office 脚本
我正在使用以下 Office 脚本在工作日结束时隐藏每日 Excel 工作表。当从 Excel 中的脚本运行时,它运行良好,但是,当在一天结束时使用 EST 23:00 触发 Power Automate 时,它会失败。我相信这是由于 Power Automate 在一天结束时使用的时区与我所在的时区存在差异,当通过 Power Automate 在一天结束时触发此操作时,是否有某种方法可以定义正确的时区?
function main(workbook: ExcelScript.Workbook) {
//Assign the "Template" worksheet to the ws variable
let date = new Date(Date.now());
let ws = workbook.getWorksheet(`${date.toDateString()}`);
//Set the visibility of the ws worksheet to hidden
ws.setVisibility(ExcelScript.SheetVisibility.hidden);
}
I am using the following office script to hide a daily excel worksheet at the end of the work day. It runs fine when run from the script in excel, however, it fails when triggered by Power Automate at the end of the day using 23:00 EST. I believe this is due to differences in time zone utilized by Power Automate at the end of the day in my time zone Is there some way to define the correct time zone when triggering this near the end of the day via Power Automate?
function main(workbook: ExcelScript.Workbook) {
//Assign the "Template" worksheet to the ws variable
let date = new Date(Date.now());
let ws = workbook.getWorksheet(`${date.toDateString()}`);
//Set the visibility of the ws worksheet to hidden
ws.setVisibility(ExcelScript.SheetVisibility.hidden);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于云来说,时区是很棘手的。您无法 100% 确定您的代码最终将在哪个时区运行。这实际上取决于云计算的数据中心位于何处及其配置方式。
当您需要将日期时间对象转换为字符串时,区域设置(文化)会让事情变得更糟。转换后的输出可能因文化而异。
在处理日期和时间时,您必须始终明确。
如果您按照问题中提到的时区信息(
23:00 EST
)明确配置了 Power Automate 流程,我认为您的 Power Automate 流程可能会在正确的时刻触发。因此,我猜您代码中的以下两个 API 调用可能是罪魁祸首:
Date.now()
返回该地区的当前日期时间(可能与您居住的地区不同)运行脚本的数据中心的名称。date。 toDateString()
返回日期对象的日期部分的文本表示形式,采用运行脚本的数据中心区域的当前区域设置(可能与您居住的区域不同)。 p>您想要做的是获取当前 EST 时间的日期部分,我想知道这是否有帮助:
您需要传入预期的时区(您可以从以下位置找到所有时区名称) <一href="https://en.wikipedia.org/wiki/List_of_tz_database_time_zones" rel="nofollow noreferrer">https://en.wikipedia.org/wiki/List_of_tz_database_time_zones)和预期的区域设置(默认情况下 <代码>en-US)。默认情况下,
America/New_York
和en-US
的日期字符串采用2/25/2022
格式。如果它与您的工作表名称的格式不匹配,您可能需要调整 toLocaleDateString()。Time zones are tricky when it comes to the cloud. You cannot be 100% sure which time zone your code will end up running in. It really depends on where the data center of the cloud compute is located and how it's configured.
And locale (culture) makes things even worse when you need to convert date time objects to strings. The converted output might be different from culture to culture.
You'll have to always be explicit when working with dates and times.
I'd assume your Power Automate flow probably gets triggered at the correct moment if you have configured it explicitly with time zone information as you mentioned in your question (
23:00 EST
).So I guess the following two API calls in your code might be the culprit:
Date.now()
returns the current date time of the region (might be different from the one you live in) of the data center where your script is running in.date.toDateString()
returns the text representation of the date part of the date object, in current locale of the region (might be different from the one you live in) of the data center where your script is running in.If what you want to do is to get the date part of the current EST time, I'm wondering if this could be helpful:
You need to pass in the expected time zone (you can find all the time zone names from https://en.wikipedia.org/wiki/List_of_tz_database_time_zones) and the expected locale (by default
en-US
). By default, the date string forAmerica/New_York
anden-US
is in the format of2/25/2022
. If it doesn't match the format of your worksheet names, you may need to tweak the parameters of toLocaleDateString().如果是我,我会从 PowerAutomate 步骤确定日期并将其用作参数,而不是直接从 Office 脚本执行此操作。
注意: 这假设您始终从 PowerAutomate 中触发此操作,并且用户不会从直接 Excel Online。
作为一个非常基本的示例,这就是我的脚本的样子......
您可以看到,我有一个
localDateTime
参数,然后可以填充该参数通过 PowerAutomate 流程。在流程本身中,我让它看起来像这样...
Local Date Time
变量表达式如下所示...该表达式为我提供了完全符合我想要的日期和时间它在我的时区。为了演示的目的,我已经精确到小时和分钟以显示其真正的准确性,正如我希望实现的那样,这就是结果......
忽略地址中的 AM酒吧,它正在处理字符串根据收到的内容将单元格作为日期/时间,但您不会这样做,您只会使用日期来显示/隐藏工作表。
如果需要将其存储在单元格中,那么我必须使字符串更严格一些,并包含 AM/PM 或使其为 24 小时之类的。
因此,如果您在 PowerAutomate 中的表达式是这样的......
或者您的工作表名称的任何 yyyy-MM-dd 格式,那么它应该按照您的需要工作。
If it were me, I'd be determining the date from a PowerAutomate step and use it as a parameter rather that doing it from the office script directly.
Note: This assumes that you're always going to trigger this from PowerAutomate and users will not trigger it from the
Automate
tab in Excel Online directly.As a really basic example, this is what my script looks like ...
... you can see, I have a
localDateTime
parameter which is then able to be populated through the PowerAutomate flow.In the flow itself, I have it looking like this ...
The
Local Date Time
variable expression looks like this ...That expression gives me the date and time exactly as I want it in my timezone. For the purpose of this demonstration, I've gone down to the hour and minute to show it's true accuracy and as I'd hoped to achieve, this is the outcome ...
Just ignore the AM in the address bar, it's treating the string in the cell as a date/time based on what it received but you won't be doing that, you'll just be using the date to show/hide your worksheet.
If it needed to be stored in a cell then I’d have to make the string a little more stringent and include the AM/PM or make it 24-hour or something.
So if your expression in PowerAutomate was like this ...
... or whatever the yyyy-MM-dd format is for your worksheet name then it should work as you need.