I'm working on a scoreboard automation process using Google Sheets, but I've come up with a little problem. I tried searching here but the question has been treated in a unclear way to me.
What am I trying to do is quite simple:
I want an automatic e-mail to be sent to a specific person IF cell value > X (threshold). I already know I need to use Google Apps Script for that, but I haven't found much interesting code lying around yet so I was wondering if you guys had an idea how that would work?
I tried something based on this thread with no success: How do I make a Google Sheet script send an email when a specific cell's value changes?
#Yvan1401,
You can use a script like this along with an installable trigger like onEdit or onChange to accomplish what you wish. To set up the installable trigger follow the steps below (found here):
From the script editor, choose Resources > Current project's
triggers.
Click the link that says: No triggers set up. Click here to add one
now.
Under Run, select the name of function you want to trigger. Under
Events, select From spreadsheet.
Select and configure the type of trigger you want to create (e.g. on
Change or on Edit trigger).
Optionally, click Notifications to configure how and when you will
be contacted by email if your triggered function fails.
Click Save.
Please see comments in code for portions to change.
function SendEmail() {
var ui = SpreadsheetApp.getUi();
var file = SpreadsheetApp.getActive();
var sheet = file.getSheetByName("Sheet1"); //Change as needed
if(sheet.getRange(5,1).getValue()>10){ //change row and column in get range to match what you need
MailApp.sendEmail("xxxxxcxxx#gmail.com", "subject", "message");
}
}
function sendMail() {
if (SpreadsheetApp.getActive().getSheetByName('Scoreboard').getRange('AL2:AL1000').getValue()<2.70) return;
MailApp.sendEmail("yvan#********.com", "******** new potential candidate available ! ", "Dear ****, a new candidate with strong potential is available for due dil !", {
name: "SOURCE NAME"
});
}
Here is an example of code that works the best and is the shortest. The thing is, I want it to scan only the cells that has changed (updated) within the column in question and not the old responses (data source is a form). What do you think i should do ? Because i don't want to erase the previous response within the same column.
edit: I use an "onChange" trigger.
Related
I'm working with google sheets and I'm trying to figure out a way of how to get the time when a cell is filled in? I would like that access this information via the google sheet script. Is that even possible?
Thank you for your time.
After reading your question, I assume the following:
You want to detect when a particular cell is modified.
When the modification is detected, you want to print a timestamp in another cell.
If my assumptions are correct, you can use the following example to achieve your requests:
CODE
function onEdit(e) {
if (e.range.getA1Notation() == "C4") {
var timestamp = new Date().toLocaleTimeString('it-IT');
SpreadsheetApp.getActive().getActiveSheet().getRange(4, 4).setValue(
timestamp);
}
}
BEHAVIOUR
The code will run when an edit is made on the spreadsheet by a user. It will check if the modified cell is the desired one (C4 in this example), and if it is a timestamp will be dropped in the time-keeping cell (D4 in this case).
OBSERVATIONS
This code works for every sheet of the spreadsheet. To make it exclusive to a sheet, you should use getSheet methods [as getSheetByName()].
Due to onEdit trigger limitations, this code won't run if the modification is made by a script (in opposition to a user edit).
The timestamp will be printed in the italian timezone because your name looks Italian to me. Please, forgive me if I mistook your timezone.
ALLUSIONS
onEdit(event object) simple trigger
Event object
toLocaleTimeString(locale)
Please, don't hesitate to write me back any additional doubts or request me further clarifications.
I am new to Google Apps Script and I don't know how to do some codes. I have two spreadsheets, 1 for the admin and 1 for the client. Every time the admin will update the cells the client side must be updated also. However, only the data in the cells are copied but not the backgrounds in it. Here are the screenshots for better understanding:
Copy value and background from Admin to Client
This requires an installable onEdit(e) trigger. You will also need the id of the client spreadsheet. Note you cannot run this from the Script Editor unless you do it from another function that supplies the event object. See this example.
function updateClient(e) {
var ss=SpreadsheetApp.openById('ClientSideSpreadsheetId');//You need to provide id here. You cannot pass other parameters to this function because it a trigger.
var sh=ss.getSheetByName(e.range.getSheet().getName());
var rg=sh.getRange(e.range.rowStart,e.range.columnStart);
rg.setValue(e.value);
rg.setBackground(e.range.getBackground());
}
I tested this on my account and it works. I suspect that you'll have trouble getting it to run so read the answer in the link I provided thoroughly before telling me that it doesn't work.
This is what the event object looks like:
{"authMode":{},"range":{"columnStart":3,"rowStart":18,"rowEnd":18,"columnEnd":3},"source":{},"user":{"nickname":"nickname","email":"email#email.com"},"triggerUid":"123456","value":"3"}
Event Objects
I am learning to use google app script. I have written code to get row number in which so far I succeeded but can't find a way to assign it to a cell or row change event trigger. Can someone help me figure out how to do this?
My code is as follows :
function onChange(event) {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var currentCell = sheet.getCurrentCell();
sheet.getRange("B1").setValue(currentCell.getRow());
}
There are no "onSelect" trigger on Google Apps Script but you could use the poll technique. Basically, you will have to use a sidebar to hold client-side code that will check for the selected cell every determined time. For code examples checkout the following Q/A
How do I make a Sidebar display values from cells?
How to detect user changing sheet?
Have you installed the trigger? onChange triggers need to be explicitly hooked up because they're considered installable triggers (opposed to simple triggers, which just require saving the .gs file).
You can manage your triggers from the Menu in your Apps Script Editor:
I have a spreedsheet, with a sheet feed by a Google Form. unfortunetly, I need special features, like conditional formatting, and data added by google form are added in a new line without format.
So, I did an other sheet, with the same columns, and I had to copy paste from the first one to the second one. I'd like this copy to be automated. So I tried to do it using Google App Script. But it seems like I did something wrong with the trigger.
Here is the code :
function creerTrigger()
{
ScriptApp.newTrigger('deplacerCommande').forSpreadsheet("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx").onFormSubmit().create();
}
function deplacerCommande(e)
{
var id = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
var feuille = SpreadsheetApp.openById(id).getSheetByName("Commande");
feuille.appendRow(e.namedValues);
}
When I run deplacerCommande() from Google App Script, the messageBox is displayed, but not when a form is submitted, so I don't think the trigger works.
Yes, I created my trigger. I did it by running the function creerTrigger() from Google App Script. I also tried deleting it, then changing the version, then recreating it, because I read it can solve the problem, but it didn't.
list of trigger
It's in French, but I'll translate the value for you :
deplacerCommande is the name of the function called by the trigger.
the second value means From the spreedsheet.
the third means sending Form
The solution was simple. The trigger triggered, but the appendRow function raised an Exception because namedValue is an object, not an arrow. using e.values instead of e.namedvalues solved the problem!
I'm trying to get a sense of the viability of replacing some of my Microsoft Excel spreadsheets with Google Doc Spreadsheets. How can I create a custom keyboard shortcut to a google app script function in a google docs spreadsheet? This is something I commonly do with VBA and Excel.
Apps Script only exposes server side events. Unfortunately, you cannot register client side events like keyboard strokes today. Please log an issue in our issue tracker
This now supported in Sheets (see https://issuetracker.google.com/issues/36752620), but not yet in Docs. See and star https://issuetracker.google.com/issues/36752620 for Docs support
Very recently (April 2018) Google launched a macro recorder that includes a way to assign a keyboard shortcut to fire a macro and a way to import existing scripts as macros. See Google Sheets Macros
NOTE: This feature is currently being rolled out so it could take few weeks to be available for all. In my case it was available first on my personal Google account and since yesterday it is available on one of my G Suite accounts.
A solution has been posted over at issue 306! For the lazy, here it is:
The new IFRAME mode in HtmlService does allow for key codes to be passed on to Add-ons...
$(document).keydown(function(e){
//CTRL + V keydown combo
if(e.ctrlKey && e.keyCode == 86){
$( '#output' ).html("I've been pressed!");
}
})
Have to click on / activate the sidebar first for that to happen.
Great news :) You can achieve custom keyboard shortcuts for google app script functions following next easy steps:
In the Google Sheets UI, select Tools > Macros > Record Macro.
Record any action. For instance, change the color background in A1. Press Save.
Save it with a random title and the preferred shortcut. You can change it later.
Select Tools > Script editor to open the script bound to the sheet in the Apps Script editor.
In the new editor tab, select View > Show Manifest File
TA - DÁ! :D Change the functionName value to the desired one ;)
Remember macro functions should take no arguments and return no values. More info at https://developers.google.com/apps-script/guides/sheets/macros#importing_functions_as_macros
Just updating, it is now possible the workaround that The Guy mentioned, trough IFRAME, you can create a sidebar, a STUB just to enter keyboard commands, treat them with jquery, and run the apropriate function, been using this already.
One possible work around for this would be to dedicate a column for "trigger text", define different text based triggers for each action you're trying to perform, and then create a function that checks the value and performs an action based on the "trigger text". You can then set an onEdit event trigger in the project's triggers under "Resources" in the script editor for your hotkey function.
The biggest downside to this approach is that it takes (at least for me) approximately 7 full seconds for the onEdit trigger to catch the change and perform the update. If you need something to process more quickly you may have to look for an alternate approach.
I've provided an example below for how to change row color based on trigger text in a designated "trigger" column. You can use this to do anything that can be done on the sheet via scripting such as change values, set font weight, copy data, or even run other functions after checking the trigger text input.
/*you will need to add an onEdit trigger to your project
for this to run when you edit the cell*/
//function to update row color using entered text in a specified "trigger" column
function hotKey(){
//get the cell you edited and the associated column and row number
var cell = sheet.getActiveCell();
var thisCol = cell.getColumn();
var thisRow = cell.getRow();
//set a range variable for the entire row
var colorRow = sheet.getRange(thisRow,thisCol,1,Cols);
//get the edited value for the cell as a string
var val = cell.getValue().toString();
//check that the edited cell is in the trigger column
if (thisCol = 1){
//update the row color based on the entered value
if(val == "g"){
colorRow.setBackground("#00ff00"); //sets row color to green
cell.clearContent(); //delete the trigger cell value
}else if(val == "r"){
colorRow.setBackground("#ff0000");
cell.clearContent();
}else if(val == "fd"){
colorRow.setBackground("#fff2cc");
cell.clearContent();
}else if(val == "pr"){
colorRow.setBackground("#ffff00");
cell.clearContent();
}else if(val == "cn"){
colorRow.setBackground("#6fa8dc");
cell.clearContent();
}
}
}
I'm struggling with a similar issue and tho I haven't got much resolved yet I think a way forward can be found thru this keypress event handler under Class Textbox
I don't know if this gets around the problem of server side only that Arun pointed out but I'm sure hoping so. Please feel free to correct my reasoning before I waste too much time trying this! :)
Long story short: write an html service which uses a key press event handler to capture keypress events and compares them to the hotkey shortcuts you want then calls the appropriate sub routine on the server side in apps script to inline the called for keystrokes, format or document changes.
As far as viability goes, not as easy as the vba you are already familiar with but once you get past the different way of doing things not really that terribly difficult.