I've created a spreadsheet for one of my classes to keep track of marks. There is a leaderboard in the class and if a student is on top of the leaderboard for 7 days or more they receive a bonus for being so spectacular.
I have 4 columns for this. The first is the MARK column. The second is their SCORE. The third is the DATE they received the hi-score. The fourth is the AMOUNT_OF_DAYS that have elapsed since they obtained a hi-score. I have a script that runs if I enter in their results. It checks to see if it is a hi-score and date stamps todays date in the DATE column. The AMOUNT_OF_DAYS column is a simple formula that calculates the amount of days that have elapsed from when the hi-score was obtained. I would like to have the MARK column populated automatically once the AMOUNT_OF_DAYS value is 7 or greater. I tried the "change" trigger event, but I don't think that is the purpose of it. I don't think onEdit would work because that's only triggered when I manipulate the spreadsheet manually I think. Any ideas? Thanks in advance.
Does your spreadsheet look like this?
You can put a conditional formula in a cell:
=if(D2>=7,"bonus Time!",0)
The above formula enters the text bonus Time! if the cell in D2 is greater than or equal to 7, otherwise it enters the value zero.
You already have a script that runs when you enter the data, and then checks if it's a high score. I don't understand why you can't extend that script to also check the AMOUNT_OF_DAYS?
Here's what I did to solve this:
I created a function in a script that would be responsible for checking to see if any values in the THE_AMOUNT_OF_DAYS column are greater or equal to 7. Under the "Resources" menu in the script editor go to "all your triggers" and then add a trigger. Specify which function you'd like to run. Make sure that it is a "time-driven" event instead of a "From spreadsheet" event. Set the time to the interval you'd like the script's function to fire at.
One thing to note is that Browser.msgbox() functions don't seem to work when the function fires via the trigger, but it will work when you run the function from the script editor manually. The only reason I mention this is because I'm sure that many of us use this to debug our scripts.
Special thanks to #teatimer for pointing me in the right direction :)
Related
I am currently working on a spreadsheet formula where 2 different codes would be generated. Here is the algorithm for the "code" to start with., but I don't know how to construct a proper excel function for it.
There are 10 digits to the code where the first 8 digits are just the date i.e. 20210328_ _
The final 2 digits are dependent on the previous records whether there are records with the same date. If so it would assign a two-digit number starting from 1 to differentiate the different records.
I have tried to use the below formula to achieve what I want but the part where it references the other spreadsheet is bothering me as I need it to be a flexible value where the value is referring to the last row of the spreadsheet. Is there a way to work around this without scripts? I am planning to deploy it on Google Sheets so App scripts solutions would also be workable but not preferable.
=IF(DAY(B2)=RIGHT(Data!A114,2),Data!A114+1,CONCATENATE(YEAR(TODAY()),TEXT(B2,"MM"),DAY(TODAY()),"01"))
FYI B2 is the date of input and Data!A114 is the part where I concern.
Here's what I came up with.
Formula(D3)=IF((TO_PURE_NUMBER(Concatenate(YEAR(A3), TEXT(A3,"MM"),DAY(A3))) - TO_PURE_NUMBER(Concatenate(YEAR(A2),TEXT(A2,"MM"),DAY(A2)))), (TO_PURE_NUMBER(CONCATENATE(TO_PURE_NUMBER(Concatenate(YEAR(A3), TEXT(A3,"MM"),DAY(A3))), "00"))) ,(D2+1))
The data for the dates starts in A3, and continues down.
Link to the Google Sheet I tried it on.
https://docs.google.com/spreadsheets/d/1bwukKFaEow4PysqcJLA9jqjKBLZcY8T1vTN5VpZo8F8/edit?usp=sharing
Let me know if this worked.
I am building a menu system with date and food columns. The screen will have a [working date] cell that controls which rows show on the screen.
When the [working date] changes the script should run and :
count the number of rows on the sheet with a date that matches the [working date]
count the number of row with matching date and blank food cells
add enough rows plugged with the date to fill the sheets screen
add additional rows if needed to allow additional entry by scrolling the screen
sort all the rows
scroll the sheets screen so that the first row for the selected date is at the top
This should all happen automatically when the user changes the [selected date.]
I am completely green on working in scripts and initially thought I could use the dcount function to get the counts. I now believe that the sheet side functions can only be used from there. I expect that in a script one will need to select a range of data and loop through it to get the counts.
I am pretty sure I could eventually figure this out but would really benefit if someone could give me a few pointers.
Thanks in advance - Joe
For the whole process you would need to use an onEdit trigger to execute a function each time the sheets is edited and use it's event information to conditionally run the code when the [working date] cell is the one edited. I would use the installable version of the trigger which has less restrictions.
For all the actions you want to achieve you'd have to use the SpreadsheetApp methods.
1 and 2) Create a TextFinder object from a Spreadsheet object and set it up to make the search. When you set up the range for the text finder, avoid using this version of the getRange method or it may throw an error.
3 and 4) With insertRows method you can insert rows to the Sheet object, and then use its getRange and setRange methods to set the values. There's no trigger available you could use to run a function when the user scrolls the view.
5) You can use the sort method. Or get the range values, sort them with JavaScript methods and then set the ordered array in the range.
6) You can use the activate method of a Range object which will put the focus in the range cell(s).
I have an index/match formula that matches a specific file based on the date value of certain cells. Here's the formula:
=IFERROR(INDEX(INDIRECT("'"&TEXT($O$3,"mm-dd-yyyy")&"'!"&"$D3:$D$500"),MATCH($D5,INDIRECT("'" & TEXT($O$3, "mm-dd-yyyy") &"'!$B$3:$B500"),0)),0)
I noticed the values did not change even when I imported a new CSV. Only way I got the values to update was to essentially re-enter the formula by dragging from top to the last cell like one would manually do.
I tried changing the recalculation time under settings, but it seemed like the setting does not apply to my formula, as I set it to every minute and nothing happened.
I thought about writing a script to have it re-enter the formulas and set it to run every day, but I'm hoping that there's a easier way to do this.
Short answer
Your formula is not being recalculated because its arguments do not change. The solution is, as you already figured out by yourself, to re-enter the proper arguments into cells that your formula references to.
Explanation
Google Sheets formulas are recalculated when
The spreadsheet is open
The function arguments changes
The functions NOW, TODAY, RAND, and RANDBETWEEN are updated are according to the spreadsheet settings, on change, on change and every minute, on change and every hour
External data functions recalculate at the following intervals:
ImportRange: 30 minutes
ImportHtml, ImportFeed, ImportData, ImportXml: 1 hour
GoogleFinance: may be delayed up to 20 minutes
Note: Some functions and custom functions doesn't allow not deterministic functions as arguments.
References
Change a spreadsheet's locale, time zone, recalculation, and language
I found an easy solution to my problem. I wrote a script to essentially re-enter the proper dates into cells that my formula references to and the formulas updated.
Here's another solution, albeit one that is computationally expensive: pass the range to be considered in the calculation to the function. That way, any time that a value changes in the passed range or that the range itself changes (such as inserting a row within the range), the formula is recalculated.
Example: Try this simple function.
function testPassRange( calcRange )
{
return calcRange.length ;
}
I have a 200k cell database in New Google sheets. I'm running QUERY() in the same spreadsheet but in another sheet.
The results I get from QUERY() is perfectly fine. The problem is: when I try to use the resulting queried data in another sheet, the resulting data is not what I see from the QUERY(). I only see a portion of it. This happens both when I use importrange() and when I use Google app script code to replace importrange.
When using code and I .getValues() the range, the data fetched is already not what it looks like in the QUERY function on the original sheet. So that's why I'm saying the actual result from QUERY is different from what I see.
The QUERY result from what I see is correct. The data set from importrange() or .getValues() is incorrect. This just happened suddenly. Before everything was working fine. It started this Friday (March 21, 2014) night EST.
Wondering if anybody else is having this same problem.
Edit: I've also tried the myimportrange() custom code provided by ahab as a work around. I get the same result as importrange(). I've exhausted what I can try I think. Might be a Google bug?
Edit 3/24/14: It seems to only a problem on formulas. I have a different part of the spreadsheet that was also not importing all of the values, but for that portion i pasted values because it will not change any more, and the problem resolved itself.
It seems like the formula calculates and I can see it but Google's server has a different number for that cell.
edit 3/24/14 10am EST: Things all came back without me doing anything. I think it was a Google issue. They must have turned something off over the weekend and turned it back on when the engineer got into the office today. Should I just delete this question? Moderator?
edit 3/28/14 10pm EST: it's happening again. This time the QUERY() function not producing the correct results.
edit 4/2/14: This problem carries over to other formulas (simple formulas)
edit 4/7/14: This seems like a recurring problem over the weekends (US time zone)
edit 4/23/14: latest cycle of importrange down started Good Friday, importrange works again now.
edit: I have a more accurate description of the error:
So let's say i have a spreadsheet foo. In cell A1, sheet1 in spreadsheet foo, is a formula that returns value X. value X is the correct value, but since value X depends on all these different calculations (such as a query, which is blank at load) from inside the spreadsheet, when you first open/load the spreadsheet, the initial value is Y. Only after about 30 seconds to 1 minute does the value (after calculating what looks like 3-4 times) settle correctly to value X. Every time i hit reload on the spreadsheet, the value gets reset to value Y, and it takes 30 seconds to 1 minute to arrive at the correct value X.
Now i have another spreadsheet bar. In bar, i'm importing the range sheet1!A1 from spreadsheet foo. However, value Y shows up, even though value X is displayed in foo. So bar is only loading the initial value but not the calculated and most recent value X.
This phenomena has been ongoing since I started using the new sheets. Sometimes it imports correctly and imports X from foo, other times it imports Y from foo. And i've done absolutely nothing. It seems to be unstable. Sometimes it is able to import the correct value, sometimes not.
I've been having a similar problem with the sum function. I have a sum formula with a dozen or so values to add. When I click on the cell with the formula, it will display the correct total in the formula bar but the result in the cell displays a previous result. The only way I've been able to correct this glitch is by refreshing the page. Personally, I think this is a ridiculous flaw as it defeats the purpose of having a spreadsheet.
I'm making a spreadsheet that updates itself daily. For simplicity, let's say I generate a number every noon, and at night the spreadsheet is supposed to copy the number into a log column, and show the sum of all stored values for this column (there are actually several columns, and their number might grow in the future). With my current implementation, the sum formula is updated automatically, and thus keeps saving only the values previously available. I wish I could prevent this behavior.
Example:
The spreadsheet reads:
Total 1
Today 2
History
1/29/2014 1
Tomorrow, before I update it, it should be:
Total 3
Today
History
1/30/2014 2
1/29/2014 1
The formula I inserted on "Total" line is:
=sum(B4:B)
Basically, the code I've made for the script contains:
//Inserts a new line to compose history log:
sheet.insertRowsAfter(firstHystoryLine-1, 1);
And after the script is run, the formula is updated to
=sum(B5:B)
Therefore my problem.
Alternatively, you can add in your code
sheet.getRange("B1").setFormula("=sum(b4:b)")
This will reset your formula correctly.
Try Sum(b3:b). Inserting a row should then not affect the formula.
set your field to be =Sum(B$4). Using the dollar sign should prevent it from sliding. If you were adding in columns as well you'd want to do =Sum($B$4).