Office Script with Power Automate to Dynamically Change Excel Worksheet Date - excel

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

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 https://en.wikipedia.org/wiki/List_of_tz_database_time_zones) and the expected locale (by default en-US). By default, the date string for America/New_York and en-US is in the format of 2/25/2022. If it doesn't match the format of your worksheet names, you may need to tweak the parameters of toLocaleDateString().

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 ...
function main(workbook: ExcelScript.Workbook, localDateTime: string) {
let worksheet = workbook.getActiveWorksheet();
worksheet.getRange("A1").setValue(localDateTime);
}
... 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 ...
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 ...
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

Docusign - date tab - formatting input

I am having difficulty specifying the format of the date tab to be inputted as dd/mm/yyyy. This is a date input tab, it is NOT the date signed tab.
I HAVE changed the account default regional settings to my time zone and changed the default date/time format for the account under regional settings. However, this appears to have no impact on the users input for the date tab upon signing.
I am using REST-api to send the tags, so please don't provide advice based on using a template! It has to be either in the account settings (of which there doesn't appear to be) or in the specification of the date tab in the code (I'm using c#).
click here to see example use
public static List<Date> CreateDateTabs(IEnumerable<Docusign_Date> DD)
{
List<Date> listTabs = new List<Date>();
foreach (Docusign_Date item in DD)
{
Date DS = new Date();
DS.DocumentId = item.DocumentGuid.ToString();
DS.RecipientId = item.PartyID.ToString();
DS.TabLabel = item.Party_Tag;
DS.AnchorString = item.Party_Tag;
listTabs.Add(DS);
}
return listTabs;
}
Your example shows a text tab, not a date tab.
I know it's confusing.
But in DocuSign if you want user to provide input date, you must use a text tab and set it such that the required format is for date.
Change your code to textTab, add formatting based on your requirement (first day (1-31), then month (1-12), then year) and you can even provide the error message if someone provides the wrong date.
Let me know if it's not clear and you need a code example.

How to create a report subscription in SSRS which passes today's date to paramters?

I created a report with a StartDate and EndDate parameter. If I want to see the information for a single day, I use the same Date in both parameters. I now want to create a subscription for this report so that it runs everyday. How can I use the current date and pass it to these parameters when the report runs? Thanks!
Step 1: You will have your defaultDates dataset as so. can be query or you can wrap it as a Stored proc.
Select TodaysDate = cast(getdate() as date)
Step 2: then , under default values for both the params you will anchor get value from dataset and point to this dataset, which is defaultDates.
Step 3: test it locally. Make sure to delete .DATA from your working directory to enforce fresh data.
Step 4: build and deploy to whatever test location.
EDIT: This will only work with Enterprise edition.
First, write a query that gets the current date and formats it to match the VALUE in your parameter (for example, is it DD-MM-YYYY, YYYY-MM-DD?). Make sure to name your column something meaningful like "CurrentDate".
select cast(current_timestamp as date) as CurrentDate
Then create a new subscription for your report. Instead of Standard Subscription, choose data driven subscription. Now select your SQL datasource and paste in your query. Press validate to make sure it runs fine. Hit OK.
Now you can go down to your subscription parameters at the very bottom of the page. Set Source of Value to be "Get value from dataset" then pick your "CurrentDate" from the drop down.
That's it, data driven subscription with current date.

Calculate Last Working Day

I am trying to calculate the last working day for a process. Typically, this will be yesterday. However, if the process runs on a Monday it should bring back the date of Friday.
My calculation is:
Today()-MakeTimeSpan(1, 0, 0, 0)
Current code stage
The Best Practice in this area is to use VBO called "Calendars", that is a Internal one, built in the BluePrism.
Object: Calendar
Action: Add working days
Calendar Name: "Working Week / No Holidays"
Date: Today()
Days -1
An advantage of that solution is that you can customize the calendar to add all Holidays that are Bank Holidays for your company.
In the past, I've been able to accomplish this by using a dedicated Utilities object with an action that's just a single code stage. The action has a single Output connected to a single output of the Code stage.
out = DateTime.Now.AddDays(-1)
While out.DayOfWeek = DayOfWeek.Saturday Or out.DayOfWeek = DayOfWeek.Sunday
out = out.AddDays(-1)
End While
This also opens the ability for you to add complex logic to account for holidays in your locale(s) without cluttering up your action page.

Excel doesn't show value as date although indicated with VT_DATE and using SystemTimeToVariantTime

we are exporting to excel (office2010) via COM automation (C++).
Meaning we use SafeArrays, VARIANTs and the use the excel API like range->PutValue().
We have an issue on certain (not all) customer machines with exporting date values.
We normally indicate the value of the VARIANT as VT_DATE and fill in the date member.
Something like this:
VARIANT vValue;
SYSTEMTIME SystemTimeVal;
short Year=2011;
short Month=11;
short Day=14;
short Hour=0;
short Minute=0;
short Second=0;
SystemTimeVal.wYear = (WORD)Year;
SystemTimeVal.wMonth = (WORD)Month;
SystemTimeVal.wDay = (WORD)Day;
SystemTimeVal.wHour = (WORD)Hour;
SystemTimeVal.wMinute = (WORD)Minute;
SystemTimeVal.wSecond = (WORD)Second;
V_VT(&vValue) = VT_DATE;
SystemTimeToVariantTime(&SystemTimeVal, &(vValue.date));
// SafeArrayPutElement related code omitted here
pRange->PutValue(variantContainingSafeArray);
// ...
When opening the xls file, the value shown is as a number.
If you open Format dialog in Excel, it should "General".
In this case the number is 40861.
When letting Excel format it as Date, the correct date 14 november 2011 is shown.
On my pc and at other customers, Excel nicely formatted it as Date.
The case in which it happened is with a French customer, but colleages have used the same regional settings and language settings, and on their pc every thing goes fine...
Also we have several other French customers and never got a complaint from them.
I suppose it must be some machine specific issue but have no clue...
Did somebody run into the same kind of issue?
Bart
it looks like installing SP1 for Office fixes the problem...

Modx assign default date to template variable

I have a template variable of input type date but I'd like to have a default value of the current date at the time the document was first saved.
I've noticed that using the date formatter widget gives an option to "If no value, use current date" however it seems to use the date at the time it is being viewed rather than the date at the time the document was created.
Any help appreciated.
I think the only way to get the date when the article was saved for the first time is to use [ * createdon * ]
If there is a way to add the created date to a TV, I am not sure. I think your best shot would be to see if you have an event that you could "hook" you Template Variable, and then use the createdon variable everytime the article is saved! I am not sure this works, but This could be a possible!

Resources