I have google sheet that i want to use every day (like a main template), where I just need to add a few values in a specific cells,
i want to be able automatically or not to set those cell values everyday (let's say at midnight) back to my default values (let's say 0 or 1).
Any clue, any help,
Thanks.
You can use script triggers to run your script every nth hour.
Triggers: https://developers.google.com/apps-script/guides/triggers/installable
Check "Managing triggers manually" section.
You will have to create a script in google spreadsheet to change the values to a default value and setup up triggers to run that script every nth hour or so.
So all I need to do was create a script for a sheet
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Sheet1");
var range = sheet.getRange("B2");
var values = range.getValues();
if(values > 0) {
range.setValue(0);
}
}
And after that Click on Resources, Current project's triggers.
Add new trigger and set it like this:
I think that's it, it is tested and work.
If something is missing please edit my answer,deleting trigger or something like that.
Related
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.
Dynamo Script
Within Dynamo I was able to adjust the title block per sheet but I was requested to simplify it into a button click using python only.
I can find the sheets but I can not adjust the parameters per sheet. I believe this is because the parameter I am trying to toggle on and off is located within the Titleblock and not on the sheet it self. I thought maybe I would need to unwrap these but I could not get the function to work outside of Dynamo. Any help would be appreciated.
sheet_collector = FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Sheets) \
.WhereElementIsNotElementType() \
.ToElements()
for sheet in sheet_collector:
print(sheet.Name)
The snippet above is how I am sourcing all the sheets and I have been able to find everyone but when searching for the custome parameter, it comes up nil.
To get the FamilyInstance of the title block you can use this:
var titleBlockFamInst = new FilteredElementCollector(doc, viewSheetId).OfCategory(BuiltInCategory.OST_TitleBlocks).FirstElement() as FamilyInstance;
I can't find the answer but it might be that I'm looking for the wrong things.
I got a sheet that got tabs for every letter in the alphabet and those includes data beginning
on that letter. I now want to copy those line into a summary sheet but before I just copy it
I need to remove the first line from every tab as that is the heading, the heading should not
appear over and over down all the lines in the new summary tab.
If I just do a variable of a tab and remove the first line, like:
var sheets = ss.getSheetByName(tabs[i]);, this will reflect into the tab itself
and that is wrong as the heading is now gone in that tab and that I can understand as it is not
a new object but a referencing variable.
I tried to create a new object like: var sheets = new Object(ss.getSheetByName(tabs[i]));
and then change that figuring it wouldn't touch the tab itself as this is now not a reference object
but it seems it is, as changing the 'sheets' variable still changes the original data in that tab.
I hope that makes it clear.
How can I create a new object of that tab and is NOT a reference but a COPY of it so I can do
whatever I want to that object without changing the original data in the tab it came from.
Perhaps something like this might work:
function summaryAZ() {
const sA=['A','B','C'];//etc
const ss=SpreadsheetApp.getActive();
const sumsh=ss.getSheetByName('Summary')
sumsh.getRange(2,1,sumsh.getLastRow()-1,sumsh.getLastColumn()).clearContent();//clear everything except header row
sA.forEach((n,i)=>{
let sh=ss.getSheetByName(n);
let [hA, ...data]=sh.getDataRange();
sumsh.getRange(sumsh.getLastRow()+1,1,data.length,data[0].length).setValues(data);
});
}
I am currently trying to fill a block with a int from a method that I wrote with the script editor.
And ofcourse I know I can just left click a block and type the number in, but I need to do more than that.
I have no experience in java script, but I code in JAVA, so I could understand a lot of it.
I have the following code inside my method:
var sheet = SpreadsheetApp.getActiveSpreadsheet();
sheet[5][15] = 60 ; // I have stripe down almost everything to where my problem occurred
I am trying to fill row 5, column O with 60. But it isn't working.
I tried sheet[5][O] = 60; but this doesn't work neither.
So how can I refer to the location 5,O on the sheet and give it a value?
sheet.getRange("O5").setValue(60);
or
sheet.getRange(5, 15).setValue(60);
In a sheet of mine I used 50 importData functions to shorten links via bit.ly automatically. Unfortunately, there is a limit of 50 functions per sheet which doesn't allow me to add more functions.
I don't want to open up more and more sheets, because that would be messy long term. Do you have recommendations for a workaround?
Create a custom function and put this to get the contents of your url:
var info = UrlFetchApp.fetch("yourURL");
Process the info and return it.
You might need to use
Utilities.sleep(1000);
...between the calls in order to get a value for every cell.