How to disable editing of cells after midnight in googlesheet? - google-apps-script

I have a google spreadsheet which we use to feed inventory purchase and issue data. The only problem is my staff can manipulate purchased quantity, prices and other variables at a later date. I want that if they enter data in a cell, they cannot edit it post-midnight but I can.
Also, I have filled most of the cells with formula, for example- there is formula in A1:H999, so that when they enter data, it calculates the values in other cells, and suppose my staff has entered data till A1:H500. In this case, the script should work On A1:H500, it should not consider the whole range just because i have filled it with formula. Thanks

If you want to limit access to the script to particular hours it's fairly simple. This can be done with one function if written in a clever way or with 2 functions.
Function1 will simply lock the spreadsheet and allow only you to edit by using the protection class, then use function2 (or write function1 in a clever way to do different things depending on when it starts) that is triggered at a different time to remove that protection.

Related

Built-in functions do not update values after script run

COUNT.IF built-in functions in Google Sheets do not update calculations after my script run.
The script (invoked by a custom push button) take values from an input mask (in another sheet), in order to insert a line in the summary sheet, which is fed by input mask data. In another sheet, where a series of COUNT.IF formulas should report the current situation, nothing happens.
The only way to refresh data is manually copying formula from another cell.
Auxiliary Notes:
main menu automatic calculations option is regularly set.
I inserted in the script the re-copying of all COUNT.IF formulas and the FLUSH instruction, but never changed.
The COUNT.IF formulas not updating are pointing to dates, which are converted into strings, since there are strings in the summary table to be counted. I am afraid this is crucial, because other COUNT.IF formulas not involving dates regularly update.
How can I get an automatic refresh of such COUNT.IF column or what am I getting wrong?
Update required by TheMaster: below the capture of the formula. (I am sorry for the "italian" but I think so is enough clear).
Formula in F3
"COUNT.IF" is in italian "CONTA.SE", while "SE" stands for "IF".
Dates are in the B column, and are pointed by the F column formulas in order to be converted into text and counted in the "colAppuntamenti" summary range.

Script to apply formulas to cell drop-down

I am making a status tracker sheet that has a column for the status of each task. Right now, Column D is has the status for each task and is set up with a drop down menu:
No action needed
Needs attention
In progress
Complete
Column E has the due date for each task.
Is there a way to write a script for the drop down menu that will change the status of cells that say "No Action Needed" to "Needs attention" once the due date is within a week of the current date?
Also, what's the best way to incorporate email notifications into the script so that any time a status changes to "Needs attention", an email is sent to the owner in Column F?
This would definitely be possible through the built-in script editor.
Overall you would want to be able to interact with the sheet that this is data is stored in. The easiest way to do this is through the SpreadsheetApp class and methods.
For the first step, you would want to set up a script that checks whether the due date is within a week of the current date. This can be done using the Javascript Date Methods which can easily be compared using regular number operations such as subtraction and addition.
The second part of the script would be possible using the built-in apps script functions of MailApp.
When you have both of these parts of the scripts down, you should tie it to a trigger to ensure that the script will trigger at either given times (time-driven triggers) or at given events (event-triggers).

Imported range time values dont match source range time values Google sheets script

I have simple google spreadsheet with two sheets: sheet1 and sheet2. In sheet2 there are time values in column A which I am trying to copy to column A in sheet2. I wrote a script(please see link) for it but values are not the same. What class/method do I need to use in order to get same values in both sheets.
I noticed if I format column A values in sheet2 to text than I get matching values but problem is I am not allowed to that and that column must remain formatted as time.
Also, my second question(please ignore if its off topic). If I have work schedule in google sheet for 3 employees: John,Peter and Mike and they all have editing permissions. If Mike decides to use filter to filter out just his work schedule, is it possible for other two employees to have unedited sheet(as I believe Mike's actions will affect other two persons). What I mean is, is it possible to restrict one persons actions just to view he is looking at while other have unaffected version(together work schedule in this case)?
link for spreadsheet:
https://docs.google.com/spreadsheets/d/1tOtwzM0CxDHBXzC8ECeXlfaTGC4i7rH2XMYYszqRINM/edit?usp=sharing
Much appreciated.
not entirely sure what the problem is, your script is working as intended and you have the same values in sheet1 as in sheet2. The only difference is that in Sheet1 you have a date format and on sheet2 you only indicate the time. I have changed the number format in the sheet1 and as you can see it's the same.
The problem you are having is that by indicating only the time, you let the sheet assume any date. For time it's not that big a deal, but you must be sure that time formats are the same. use getNumberFormat() and setNumberFormat() (read here) to set the correct formats when copying or have the formatting set in advance
As for filtering — you can simply give them view-only access and let them use filtere views (right under "Filter"). A filter will hide information for everyone on the sheet, while a filter view will only be visible to that person. You can also pre-set filter views for them as those with edit access can save filter views.

How do I efficiently output to a non-contiguous range in Google Apps Script (GAS)

I'm new to Google Script, so appreciate your help!
Here's what my data looks like (3 non-contiguous records, 4 non-contiguous fields):
https://docs.google.com/spreadsheets/d/18FFB2HlcfcciHj7NPmihZbuf47op2UMdRTKfpyTqowU/edit#gid=0
I have an array of the items and each item is an object that contains 4 keys. I want to output to Google Sheets in as few SetValue requests as possible. If I can't do it in 1 call, then it makes most logical sense to output each item at a time.
My idea is that I can create ranges for fields 1 and 4 that span the entire column. Then I can create ranges that span the entire Item row. Then the INTERSECTION between the 2 is the range I want to output to, once I have assembled an array of 2 values.
Or perhaps since assuming I know the rows/columns of each cell below, I can return the range and the use a UNION of the ranges to create the mapping instead.
But is there a function to do Intersection or Union in GAS? Or am I better off just outputting each cell 1 by 1?
Thanks for your help!
There's no such function. It's either one by one or contiguous cells.
But there's quite a few tricks/alternatives that might work for you. As Sandy pointed in a comment, you could get a contiguous range that had all required cells and set the non-required ones to blank or their original values. This has the down side of not working for formulas.
You could also pre-configured the required cells with simple formulas "pointing" to a continuous range. That would simply bulk update at once.
If bulk setting is really required, you could also grab all values and formulas of the wide range, and convert all plain values to their equivalent formulas, e.g. abc becomes ="abc" and 1/1/2015 =DATE(2015,1,1) (yes, it's cumbersome) and then use setFormulas to set everything back, both original formulas and values converted to formulas. Making no actual content change in the cells you don't want to and changing the required ones, all in one bulk operation.
Anyway, these are just workarounds. As stated in the first sentence, it's not possible, period. You probably want to star this report in Apps Script issue tracker to kind of vote for this feature and receive updates.

Is there some way to populate all the cells from 1 function call instead of 1 call for each cell?

Here is my issue. I have a spreadsheet with multiple sheets, and each sheet has about 300-500 rows. I am using ScriptDb to store the data for each sheet.
What I am currently doing is calling a custom function in 300-500 cells in each sheet to populate certain cells with data, and what happens is that some will populate and the rest will error out saying i've queried the database too many times in a short period. Obviously having to query the database for each cell isn't the best solution.
How would I go about querying all the data for the current sheet and then having that data available to grab for each cell. What I've read is that you can't really have "global" variables in GAS, but have to use things such as CacheService or ScriptDB, which is what i'm trying to do. I'm just querying it too much.
Is there some way to populate all the cells from 1 function call instead of 1 call for each cell? What am I missing or what other solutions are there?
Just realized a similar question was asked earlier today: Google Spreadsheet Script invoked too many times per second for this Google user account
Yes its possible. Simply return an array from your function. It will work like an arrayformula.
Of course your cells would need to be contiguous.