Google Sheet Script - onChange - javascript

Please help with Google onChange function Sheet Script.
I only want to log onChange for one sheet "Attendance" the change on this sheet is then logged in "Sheet5"
When there is a change in "Attendance" column C, then copy the value of column B into "Sheet5" and add the current date and time. Here is some code I tried, but do not know how to change it.
function createSpreadsheetOpenTrigger() {
ScriptApp.newTrigger('onChange')
.forSpreadsheet('ID to go here')
.onChange()
.create();
}
function onChange(e) {
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet5");
const lastRow = sheet.getLastRow();
sheet.getRange(lastRow + 1,1,1,4).setValues([
[e.changeType,
e.source.getSheetName(),
e.source.getActiveSheet().getActiveRange().getValue(),
new Date()]
]);
}

Try this:
function onEdit(e) {
const sh=e.range.getSheet();
if(sh.getName()=='Attendance' && e.range.columnStart==3 && e.range.rowStart>'Your header row') {
e.source.getSheetByName("Sheet5").appendRow([e.range.offset(0,-1).getValue(),new Date()]);
}
}
onedit event obj

Related

how to make this translation function code by google apps-script?

I really want to add a translation menu on my google sheet but I definitely don't know what should I do at the last part.
function onOpen(e) {
SpreadsheetApp.getUi()
.createMenu('Translation')
.addItem('ko to eng', 'myFunction')
.addToUi();
}
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getActiveRange();
var trans = LanguageApp.translate(sheet,'ko','en');
that is it for now.
I want to make 'myFuncton' that can spread var 'trans' at the same place in the sheet.
how can I code this?
If you'd like to print the translation e.g. on cells to the right of the selected cells, you can use this function:
function myFunction () {
var range = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getActiveRange();
var trans = range.getValues().map(row => [LanguageApp.translate (row,'ko','en')])
range.offset(0,1).setValues(trans)
}

Get link to sheet in image

how can i go to a specific sheet when i click on the image? I need to go to the "FERMENTADOR 2_002 / 2021" sheet when the user clicks on the "fermenter" image.
the problem is that the name of the sheet may change according to some criteria ... so the sheet to be activated needs to be with the value of cell A4 + the value of cell B16
function hyperlink(){
var sheet = SpreadsheetApp.getActiveSheet();
var nomeferm = sheet.getRange("A4").getValue();
var numlote = sheet.getRange("B16").getValue();
var pagina = nomeferm+"_"+numlote;
var tt = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(pagina);
}
Updated Code based on your comment:
function getToSheet() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName('HOME');
const name = sh.getRange('A4').getValue()+"_"+sh.getRange('B16').getValue();
const sheet = ss.getSheetByName(name);
if(sheet){
ss.setActiveSheet(sheet, true);
}
}
Solution:
I guess the most straightforward way is to attach the following very simple script to your image:
function getToSheet() {
const ss = SpreadsheetApp.getActive();
ss.setActiveSheet(ss.getSheetByName('FERMENTADOR 2_002/2021'), true);
}
The script will activate sheet FERMENTADOR 2_002/2021 therefore it will redirect you there.
You just need to assign this script to your image and then you can click on the image itself (see attached gif).
Illustration:

Trying to pull user email when they check a checkbox

I am trying to pull an active user's email when they check a box. I wish to paste the active user's email in a separate column (adjacent currently, but the ability to be flexible would be nice). I have done this before in a different worksheet, but for some reason I cannot get it to work on this one. I can get it to paste a simple variable, or even the edit date, so I am thinking there must be something erroring out with the user specifically. I am on a work owned domain with company addresses, but so is the other sheet that is working. Anyway, here is what I have currently (amature, be gentle):
function onEditAdded(e) {
var activeSheet = e.source.getActiveSheet();
if (activeSheet.getName() == "New Hires") {
var aCell = e.source.getActiveCell(), col = aCell.getColumn();
if (col == 20) {
var dateCell = aCell.offset(0,1);
if (aCell.getValue() === true) {
var email = Session.getActiveUser().getEmail();
Logger.log(email);
dateCell.setValue(email);
} else {
dateCell.setValue("");
}
}
}
}
I test your code and it should run without issues.
You just need to add it as an Installable Triggers
Manually create installable trigger:
Open your Apps Script project.
click Triggers alarm.
At the bottom right, click Add Trigger.
Select and configure the type of trigger you want to create.
Click Save.
function onEditAdded(e) {
var activeSheet = e.source.getActiveSheet();
if (activeSheet.getName() == "New Hires") {
var aCell = e.source.getActiveCell(), col = aCell.getColumn();
if (col == 1) {
var dateCell = aCell.offset(0,1);
if (aCell.getValue() === true) {
var email = Session.getActiveUser().getEmail();
Logger.log(email);
dateCell.setValue('user#example.com');
} else {
dateCell.setValue("");
}
}
}
}
Output:
You can also explore on how to create installable triggers programmatically, so that you wont need to add it manually every time you copy this script to a different workbook.
References:
https://developers.google.com/apps-script/guides/triggers/installable#managing_triggers_programmatically
Is there any way to automatically create installable triggers?

Issues with Triggers on Google Script

I have the following function which works perfectly fine when I run it manually.
function LastUpdate() {
var thisSS = SpreadsheetApp.getActiveSpreadsheet();
var sheet = thisSS.getSheetByName('Roster');
var data = Session.getActiveUser().getEmail();
var Edit = 0;
var time = TimeStamp();
var Row;
Row = findInColumn("L", data);
Edit = checkEdits(Row);
if (Edit == 1)
{
sheet.getRange(Row,20).setValue(time);
}
}
I set up a trigger manually by going to Resources -> Triggers and selected the above Function to execute onEdit. Basically, whenever a cell in the sheet is edited, I want to automatically trigger this function to execute. I am not sure what the issue is !
Any help would be much appreciated.
EDIT - I tried to do this and this also does not work
function onEdit(e)
{
LastUpdate ()
}
Why don't you use the simple onEdit trigger instead of the installed trigger? Since you're looking for the latest edit made by the active user that might be a much easier, faster and more stable solution.
function onEdit(e){
var range = e.range; //edited range
range.getColumn();
range.setValue();
//etc
}

Google Spreadsheet Custom Menu Returned Script Function Not Found

I am trying to add a custom menu to the google spreadsheet editor's interface to activate a script by Tony Hirst to pull calendar events into the spreadsheet.
By when I click on the custom menu option, it returned "script function not found: GetCalendar()"
I have tried to make modifications and read the google documentation but am not sure what I am doing wrong.
Thank you.
/**
* A custom function that gets the calendar events by calendar ID.
*/
function GetCalendar() {
// get the spreadsheet object
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
// set the second sheet as active
SpreadsheetApp.setActiveSheet(spreadsheet.getSheets()[1]);
// fetch this sheet
var sheet = spreadsheet.getActiveSheet();
//http://www.google.com/google-d-s/scripts/class_calendar.html#getEvents
// The code below will retrieve events between 2 dates for the user's default calendar and
// display the events the current spreadsheet
var calId = "SpecificCalendarID";
var cal = CalendarApp.getCalendarById(calId);
var events = cal.getEvents(new Date("June 15, 2015"), new Date("March 20, 2080"));
for (var i=0;i<events.length;i++) {
//http://www.google.com/google-d-s/scripts/class_calendarevent.html
var details=[[events[i].getTitle(), events[i].getDescription(), events[i].getStartTime()]];
var row=i+2;
var range=sheet.getRange(row,1,1,3);
range.setValues(details);
}
}
/**
* A function that runs when the spreadsheet is open, used to add a
* custom menu to the spreadsheet.
*/
function onOpen() {
var ui = SpreadsheetApp.getUi();
var menuItems = ui.createMenu ('Custom Menu');
menuItems.addItem ('Get Events','GetCalendar()');
menuItems.addToUi();
}
Remove the parenthesis from GetCalendar()
Should be:
menuItems.addItem ('Get Events','GetCalendar');

Categories

Resources