Google Spreadsheet replacing a value by another value upon edit by user - google-apps-script

I have just started to find out about Google Spreadsheet Scripts, but I can't figure out how I go about doing this:
I am looking for a script that replaces just one specific cell content value ("icecream") for another value ("donut") upon editting/adding.
So far I have found this piece of script on Stackoverflow, but this only works on a cell (not the value in the cell ie. "icecream").
And it only works on me clicking play (I want it to do it automatically, everytime a user enters the specific value into a box. So, if a user enters "icecream" into a cell, it needs to automatically be replaced with "donut"). It doesn't matter which cell the value "icecream" is in, just change the word "icecream" to "donut" automatically for the entire document, everytime someone types in the word "icecream".
Can someone help me?
function doTest() {
SpreadsheetApp.getActiveSheet().getRange('E3').setValue('donut');
}

This worked for me. Let me know if it works for you.
function onEdit(event) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var active = sheet.getActiveCell();
var cell = active.getValue();
//if cell's value is equal to icecream then set the active's value to donut
if(cell === "icecream"){
active.setValue("donut");
};
};

Related

Google-sheet function for cell above in costumized formula

I am trying to create a function/formula/script in Google sheets for my own formula.
I have one, where I need the value displayed in the cell above, which I type in manually. The formula is 5.5^(39-XX). So if for example cell B5 has the value 32, in cell B6 I'd like to type something like "=MYFORMULA" into the cell where the calculated value should be displayed. MYFORMULA should be programmed as 5.5^(39-cellabove).
I have already tried
function MYFORMULA() {
var spreadsheet = SpreadsheetApp.getActive();
var sheet = spreadsheet.getActiveSheet();
var cellRange = sheet.getActiveCell();
var selectedColumn = cellRange.getColumn();
var selectedRow = cellRange.getRow();
Logger.log(`selectedColumn: ${selectedColumn}`);
Logger.log(`selectedRow: ${selectedRow}`);
Logger.log(`selected cell vale: ${cellRange.getValue()}`);
cellRange.setValue('=5.5^(39-(MYFORMULA[-1;]))')
}
which does not work, unfortunately. I have the feeling, I have to program a function for the current cell as well. Any suggestions for the code? I am coding into Apps Script in Google tables.
Also, I have to activate a trigger in order to have premission using any newly created function. If you have any suggestions here, too, I would appreciate your help.
Cheers!
Rather than detect the active range and set a value directly, you can implement MYFORMULA as a custom function, which can take an input that you pass as a cell reference and return a value, which will be written into the cell containing the formula.
Suggested change
Change your function to include an input variable, and to return a value, without any reference to specific cells, like this:
function MYFORMULA(x) {
return 5.5**(39-x)
}
(Note ** is the Javascript symbol for exponent)
Then, in your spreadsheet, in cell B6, type =MYFORMULA(B5). The spreadsheet will pass the value in B5 to your function, and will write the return value in B6.

Selected cell does not update when using button linked to script

I have a sheet for users to enter a cash-count and their initials then hit a button which runs a script and stores these inputs in a table.
The problem is, if the user types the information (e.g. their initials) and hits the button without first pressing Return or selecting another cell, the information is not saved.
I've seen a similar post:
How to force flush a user's input?
and tried the solutions, some don't work and none are really what I'm looking for. I know that I could use a Checkbox instead of a button but I'd like to find a way do it without resorting to that. The table is a cash-count for a till so not all cells require a value (there may be no $100 notes for example) and I won't know which was the last cell edited.
[The Data][1]
[1]: https://i.stack.imgur.com/mdKXk.png
The Code:
function Accept() {
var ss = SpreadsheetApp.getActive();
var sheet = ss.getActiveSheet();
var db = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("DATA");
var vals = db.getRange("B:B").getValues();
var last = vals.filter(String).length;
var ui = SpreadsheetApp.getUi();
var user = ui.prompt("Notes:");
var values = [[
sheet.getRange("I7").getValue(), //Date/Time
sheet.getRange("N16").getValue(), //Amount
sheet.getRange("E17").getValue(), //Initials
user.getResponseText(), //Notes
]];
db.getRange(last+1,2,1,4).setValues(values);
}
One solution is to utilize a checkbox as the Accept button, and use an onEdit(e) simple trigger to run your Accept() function.
See the checkboxButtons_ script for sample code.

Google Script Move selection to specific cell

I'm trying to move the cursor (select cell) when the user clicks on the Sheet.
I've tried so many options and none of them work. The cursor just stays where I click, instead of moving to the specified cell.
Note: I change the background color of the clicked cell just to make sure the selection trigger is working.
function onSelectionChange(e) {
var range = e.range;
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
range.setBackground("blue");
sheet.setCurrentCell("a1").activate();
}
Any help greatly appreciated.
The setCurrentCell method is never executed because you are using the wrong parameter for it.
According to the documentation here, setCurrentCell expects an object of type 'Range`, however, you are passing a string to it.
In order to fix this, you should update your function to this:
function onSelectionChange(e) {
var range = e.range;
var sheet = range.getSheet();
range.setBackground("blue");
var cell = sheet.getRange("A1");
sheet.setCurrentCell(cell);
}
Also, since you are using the onSelectionChange trigger, for best practices, it is recommended you make use of the e event object, hence the modifications above.
Reference
Apps Script Spreadsheet Class - setCurrentCell(cell);
Apps Script Event Objects.

Google sheet script to add a row above a specific cell once that has been filtered out

I've been trying to build a spreadsheet where a button, once it's been clicked, it would build a new row above a specific cell containing a fixed value/sting.
Practical example would be a sheet containing cells with different values and the script should run through all the cells, find a cell with a specific string and add an empty row above that.
I've been looking around for a similar script to tweak and adapt to my situation but all of my attempts have been unsuccessful so far.
This is what I've got:
function AddRowAbove() {
var ss = SpreadsheetApp.getActiveSpreadsheet(); //get the active spreadsheet
var sheet = ss.getActiveSheet(); // get the active sheet
var rangeData = sheet.getDataRange(); //get all the data in the spreadsheet (is that the right way to do this??
// for every data within the range of data variable
for (var i = 0; i< rangeData.length ; i++){
if(rangeData[i] == "TOT") // Check to see if cell has "TOT" as value
{
ss.insertRowsBefore(spreadsheet.getActiveRange().getCell(rawData[i]), 1); // is that the right function to call???
}
}
Though logically this seems to be correct, I'm not used to script with this programming language so it's hard for me to find where the problem is.
Any help would be greatly appreciated.
Also, can anyone point me out to any resource I can study to get the basic of this scripting language??
Thanks!

Google SS: how to make a self-altering cell value for memoizing dates?

Crossposted # Google SS forums
I want do do this:
function memoizeDate(condition) {
if (condition) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var cell = sheet.getActiveCell();
cell.setValue(Date.now());
}
}
So that in (say) cell C2, if you set its value to =memoizeDate(eq(b2,"foo")), then as soon as b2 gets changed, C2 changes itself to the memoized value.
However, I get a "You do not have permission to call setValue" error, and I'm also not sure if the "active" cell (from the script's POV) is the one from which the script was called (i.e. C2) rather than who knows what else (B2? whatever random thing might be selected by the user?).
Have you tried like this (suggestion) :
function memoizeDate(condition) {
if (condition) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var cell = sheet.getActiveCell();
return new Date();
}else{
return 'nop or whatever'
}
}
EDIT following your comment : I keep reading your question and comments but I'm not sure what you really want to do.
On one hand you say "overriding the function itself" which means in fact returning a value (and that's what my code does) and on the other hand you try to get the active cell which is the one the user is editing (B2 in your example) and in that case you simply can't do that , please read the documentation about custom function :
Custom functions return values, but they cannot set values outside the cells they are in. In most circumstances, a custom function in cell A1 cannot modify cell A5. However, if a custom function returns a double array, the results overflow the cell containing the function and fill the cells below and to the right of the cell containing the custom function. You can test this with a custom function containing return [[1,2],[3,4]];.
All in all I think this question is rather unclear, (no personal offense) you should try to reformulate it at the least.