How do get custom functions to recalculate in google sheets? - google-sheets

I created a custom function for google spreadsheets. All it does is return a random letter. The function works great when I first enter it into a cell. But now I want to be able to "recalculate" the function using a keyboard shortcut; I'd also be willing to refresh the page if needed.
TLDR: I want to be able to hit a key and have my custom functions recalculate.
How can I accomplish this?
Edit to add:
Here is the code for my function.
//returns a random letter suitable for use in function notation
function ranFunLet() {
var letters = ['a','b','c','d','f','g','h','j','k','m','n','p','q','r','s','t','u','v','w','x','y','z']
var letter = letters[Math.floor(Math.random()*letters.length)];
//console.log(letter);
return letter;
}
I would like the cell I use it in to run the function again when I press a button (or refresh the page).

Suggestion
The Apps Script editor does support keyboard shortcut trigger as per this existing answer. However, you may want to try Importing functions as macros, then you can assign a unique keyboard shortcut to it.
Here's a sample
Sample Sheet
Sample script function to test
This sample script function increments the number on A1 cell.
function sample() {
var data = SpreadsheetApp.getActive().getActiveSheet().getRange("A1").getValue();
var res = data+1;
SpreadsheetApp.getActive().getActiveSheet().getRange("A1").setValue(res);
}
Import the function on your spreadsheet (in my testing it is named as sample):
In the Google Sheets UI, select Tools > Macros > Import.
Select a function form the list presented and then click Add
function.
Select clear to close the dialog.
Select Tools > Macros > Manage macros.
Locate the function you just imported in the list. Assign a unique
keyboard shortcut to the macro. You can also change the macro name
here; the name defaults to the name of the function.
Click Update to save the macro configuration.
Result
After pressing the sample shortcut key Ctrl + Alt + Shift + 2, the function incremented the number on A1 cell from 1 to 2:
NOTE: You can not choose a specific shortcut & if you'll edit your function on the Apps Script editor, you would need to re-import your function again as a macro.

the button solution is done like this:
https://www.youtube.com/watch?v=yaBMsSpAxYM

Related

How do I use the split function in Google Sheets correctly?

I want to get data from the text found in 2.. In 1. Is the data I have available and in 3. You will find the wanted result.
I have the following information available
Categories
Kleur
Soorthek
Bevestigingswijze
I am getting this text from scraping: BevestigingswijzeKlembevestigingKleurWitSoorthekSpijlenhek
I want this as a result by using a function in Google Sheets.
Wanted Result
KleurWit
SoorthekSpijlenhek
BevestigingswijzeKlembevestiging
Thank you in advance!
You could accomplish this with an Apps Script custom function. To achieve this, follow these steps:
In your spreadsheet, select Tools > Script editor to open a script bound to your file.
Copy this function in the script editor, and save the project:
function splitScrape(categories, scrape) {
const indexes = categories.map(c => scrape.indexOf(c[0])).sort();
const split = indexes.map((index, j) => scrape.slice(index, indexes[j+1]));
return split;
}
The sample above won't detect multiple occurrences of the same category in the scrape string, and it will only handle the first one (if a second parameter is not provided, indexOf only detects the first occurrence). In order to detect multiple occurrences, I'd suggest replacing the function with this one:
function splitScrape(categories, scrape) {
const indexes = [];
categories.flat().filter(String).forEach(c => {
let index = scrape.indexOf(c);
while (index > -1) {
indexes.push(index);
index = scrape.indexOf(c, index + 1);
}
});
indexes.sort((a, b) => a-b);
const split = indexes.map((index, j) => scrape.slice(index, indexes[j+1])).filter(String);
return split;
}
Now, if you go back to your spreadsheet, you can use this function like any in-built one. You just have to provide the appropriate ranges where the Categories and the scrape string are located, as you can see here:
Reference:
Custom Functions in Google Sheets

Make a cell in google sheets automatically 'insert comment'

I don't think I explained it well, column L is where I can log what I have done - with a date and time, so say I send a message, I would log MESSAGE SENT then say I make a call I could log CALLED and any info I wanted to remember - each entry being date and time stamped
In the image you can see the comment field - all i want is that to pop up when I click on the cell and be able to add to the comment field
You can use the onSelectionChange(e) trigger and check if the selected cell(s) fall into your criteria, if so then insert comment.
https://developers.google.com/apps-script/guides/triggers
You could use this sample script to set a comment on an edited cell:
function onEdit(e){
var range = e.range;
range.setNote('Last modified: ' + new Date());
}
However this requires that you edit the content of the cell to trigger the onEdit() function. Which you can modify instead to trigger the function manually (or assign the script to an image to act as a button) like so.
function setComment() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
ss.getRange('L14:L16').setNote('Last modified: ' + new Date());
}
Reference:
https://developers.google.com/apps-script/reference/spreadsheet/range#setNote(String)
https://developers.google.com/apps-script/guides/triggers#onedite

Increment +1 cells with True value

I'm trying to take a set of names with check boxes next to them and make a system so that you can check some of the names (mark them as "True") and click a button. It would then increment +1 the value next to the names of the people marked true.
Here is a link to a sample sheet:
https://docs.google.com/spreadsheets/d/1gf-BrXXR0cAYCn7bMkvvK65R290NXbP9D6aA68c06C8/edit?usp=sharing
If column A, row 2 (Tim's row) is marked true, I want to increment the value in column C, row 2 by one, so Tim would have a running total of tardies next to his name.
I hope this is do-able. Thanks!
(Now I know what you're trying to get)
In order to increment a value via the press of a button, as far as I know you have to use scripts (Tools -> Script Editor). Here's something I threw together:
// editCell takes the cell to edit and it's new value
function editCell(cellName, value) {
SpreadsheetApp.getActiveSheet().getRange(cellName).setValue(value);
}
// getCell takes the cell's value and returns it
function getCell(cellName) {
return SpreadsheetApp.getActiveSheet().getRange(cellName).getValue();
}
// plusOne adds one to the field supplied. It's linked to the button in the sheet
function plusOne() {
editCell("C2",getCell("C2")+1);
}
In order to make it work, you may need to change the targeted Cell (currently C2). You'll also need to create a drawing (Insert -> Drawing) which will act as the button you'll be able to press. Once inserted, click on the three dots on it and click on Link Script. Type in plusOne. When executing it the first time, it'll ask you to authenticate the use of scripts.
That should do the trick. I hope you have some understanding of Java Script though (to modify the code to your needs optimally).
Edit - Expandable version
So, to make every number behind a ticked field increase by one, you can use this version of the code:
// Adds one to every field within "AddArea" that has a tick in front of it. It's linked to the button in the sheet.
function plusOne() {
var ss = SpreadsheetApp.getActiveSheet();
var range = ss.getRange("AddArea");
var values = range.getValues();
var newValues = [];
for (var i = 0; i < range.getNumRows(); ++i) {
var row = values[i];
if(row[0]) {
newValues.push([true, row[1]+1]);
}
else {
newValues.push([false, row[1]]);
}
}
range.setValues(newValues);
}
You need to define a custom named area, named "AddArea" (Data -> Labeled Areas [or similar]), link the script to a button and allow the script to be run. This was hard but very fun to figure out.
Example Sheet for reference (updated)
Can be achieved with just, for example for C2:
=A2+C2
but you would need to turn on iterative calculation (File > Spreadsheet settings... > Calculation [Max. 1 is adequate]) and I would not really recommend that over a trigger with Google Apps Script.

Need assistance with a basic reset script for google spreadsheets

hello i need some help debugging this simple script i compiled to TRY and reset cells F3 all the way down to F338 in google spreadsheets.
function ClearRange()
{
if('H20' => 1)
{
sheet.getRange('F3:F338').clearContent();
sheet.clearcontent('H20');
}
}
more or less i want it to run on edit, if on edit cell H20 is greater then one (would be awesome if it was anything other then blank but im unsure how to do that) then it would set the range to blank cells. i would then like it to reset cell H20 back to blank or 0 then end the script.
To return the value of a cell, use something like this:
var sheet = SpreadsheetApp.getActiveSheet();
var cellValue = sheet.getRange('H20').getValue();
You need to manually declare the sheet variable if you want to use .getRange().getValue() or .getDataRange() and a bunch of other stuff.
Also note that just typing in 'H20' doesn't return the cell value, it's just interpreted as a string of text.
To detect if H20 is anything other than blank, you could test if the cell value's length is not 0:
if(sheet.getRange('H20').getValue().length != 0)
To also do a .clearContent() on cell H20, you'll need to do the same thing you did with cells F3:F338:
sheet.getRange('H20').clearContent();
To make the function run on an edit, click on the clock-like icon in the script editor, next to the save icon, and create a new trigger for ClearRange.

Can you write a Google Sheets function that draws something?

Is it possible to write your own custom function in google sheets script that returns a drawn image, similar to how the SPARKLINE function works, except I want to make one that draws a pie chart instead.
I do not want to use Insert > Chart... > Pie Chart because that creates a floating chart on top of the spreadsheet. I would like to be able to write my own function that would return a pie chart that is embedded within the cell that the function is entered in, just like you can do with columns, bars, and line charts using sparkline.
How about following idea? This sample script embeds a chart to a cell using custom function on Spreadsheet. I think that this method is one of various ideas.
Problems :
When you want to create a chart and embed it to a cell using custom functions, you notice that insertChart() cannot be used. There are some limitations for using custom functions. But insertChart() creates floating charts. So in order to embed a chart to a cell, the function =IMAGE() is suitable for this situation. Here, setFormula() for setting =IMAGE() and DriveApp.createFile() for creating images from charts also cannot be used for custom functions.
Solution :
In order to avoid these limitations, I used Web Apps.
To use this sample script, please deploy Web Apps as follows.
On the Script Editor,
File
-> Manage Versions
-> Save New Version
Publish
-> Deploy as Web App
-> At Execute the app as, select "your account"
-> At Who has access to the app, select "Anyone, even anonymous"
-> Click "Deploy"
-> Copy "Current web app URL"
-> Click "OK"
When it deploys Web Apps, the approval required authorization can be done, simultaneously.
Sample Script :
Please copy and paste this script to a bound script of spreadsheet.
var folderId = "### Folder ID ###"; // This is a folder to save images.
var webappsurl = "https://script.google.com/macros/s/######/exec"; // Here, please put "Current web app URL".
function embedChart(range) {
var ac = SpreadsheetApp.getActiveSheet().getActiveCell();
var q1 = "?datarange=" + range;
var q2 = "&row=" + ac.getRow();
var q3 = "&col=" + ac.getColumn();
var url = webappsurl + q1 + q2 + q3;
UrlFetchApp.fetch(url);
}
function doGet(e) {
var sheet = SpreadsheetApp.getActiveSheet();
var chart = sheet.newChart()
.setChartType(Charts.ChartType.PIE)
.addRange(sheet.getRange(e.parameters.datarange))
.setOption('height', 280)
.setOption('width', 480)
.setOption('title', 'Sample chart')
.build();
var file = DriveApp.getFolderById(folderId).createFile(
chart.getAs('image/png').setName("chart_image.png")
);
file.setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.VIEW);
sheet.getRange(e.parameters.row, e.parameters.col).setFormula(
'=IMAGE("' + "http://drive.google.com/uc?id=" + file.getId() + '")'
);
}
Flow of Script :
embedChart()
Input =embedChart("a2:a6") in cell B7.
Using fetch(), sends data of a2:a6 and the inputted coordinate to doGet().
doGet()
Using doGet(), get the data.
Creates a chart using inputted range a2:a6. (in this case, creates a pie chart)
Saves a chart as an image. (in this case, saves as PNG)
Updates a permission of the image file to use for =IMAGE().
Embeds the image using =IMAGE() which was imported by setFormula().
Result :
By inputting =embedChart("a2:a6") in cell B7 as a custom function, following result can be obtained.
Note :
When the custom function embedChart() is used, loading time is about 40 seconds. (I don't know whether this occurs at only my environment.)
Permissions of the created image are ANYONE_WITH_LINK, VIEW.
embedChart() is overwritten by =IMAGE(). So when the spreadsheet is reopened, the response of =IMAGE() is much faster than that of embedChart().
If I misunderstand your question, I'm sorry.

Resources