Home > other >  Office Script with Power Automate to Dynamically Change Excel Worksheet Date
Office Script with Power Automate to Dynamically Change Excel Worksheet Date

Time:02-27

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);
}

CodePudding user response:

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 can find the time zone names from https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
function currentDateStringInTimeZone(timeZone: string, locale: string = "en-US"): string {
  return new Date().toLocaleDateString(
    locale,
    {
      timeZone: timeZone
    });
}

function main(workbook: ExcelScript.Workbook) {
  //Assign the "Template" worksheet to the ws variable
  let ws = workbook.getWorksheet(`${currentDateStringInTimeZone("America/New_York")}`);

  //Set the visibility of the ws worksheet to hidden
  ws.setVisibility(ExcelScript.SheetVisibility.hidden);
}

You need to pass in the expected time zone (you can find all the time zone names from Flow

The Local Date Time variable expression looks like this ...

formatDateTime(convertFromUtc(utcNow(), 'AUS Eastern Standard Time'), 'yyyy-MM-dd hh:mm')

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 ...

DateTime

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 ...

formatDateTime(convertFromUtc(utcNow(), 'Eastern Standard Time'), 'yyyy-MM-dd')

... or whatever the yyyy-MM-dd format is for your worksheet name then it should work as you need.

  • Related