multiple timestamps - google spreadsheets appscript - javascript

Need help with my code. I need a timestamp for two status columns.
I need to stamp a date on column Labeling 1 when the status is In progress and another one on Labeling 2 when the status is Done. Same thing with QA 1 and QA 2.
Here's what I've done:
function onEdit(d){
var sheet = SpreadsheetApp.getActiveSheet();
var actRng = sheet.getActiveRange();
var rowIndex = actRng.getRowIndex();
var editColumn = actRng.getColumn();
if(sheet.getName() == "Dominios 2023" && rowIndex>1){ //Cambiar nombre de hoja
var headers = sheet.getRange(1,1,1,sheet.getLastColumn()).getValues();
var valor = actRng.getValue();
var statusCol = headers[0].indexOf('Status')+1;
var tiempoActual = new Date();
var datestartCol = headers[0].indexOf('Labeling 1')+1;
var datendCol = headers[0].indexOf('Labeling 2')+1;
if(editColumn==statusCol){
temporizadorLbl(sheet,rowIndex,valor,tiempoActual,datestartCol,datendCol);
}
}
}
function temporizadorLbl(sheet,rowIndex,valor,tiempoActual,datestartCol,datendCol){
if (valor=='IN PROGRESS'){
sheet.getRange(rowIndex,datestartCol).setValue(Utilities.formatDate(tiempoActual,'GMT-3','dd/MM/yyyy'));
}
if(valor=='DONE'){
sheet.getRange(rowIndex,datendCol).setValue(Utilities.formatDate(tiempoActual,'GMT-3','dd/MM/yyyy'));
}
}
function onEdit(g){
var sheet = SpreadsheetApp.getActiveSheet();
var actRng = sheet.getActiveRange();
var rowIndex = actRng.getRowIndex();
var editColumn = actRng.getColumn();
if(sheet.getName() == "Dominios 2023" && rowIndex>1){ //Cambiar nombre de hoja
var headers = sheet.getRange(1,1,1,sheet.getLastColumn()).getValues();
var valor = actRng.getValue();
var statusCol = headers[0].indexOf('Status QA')+1;
var tiempoActual = new Date();
var datestartCol = headers[0].indexOf('QA 1')+1;
var datendCol = headers[0].indexOf('QA 2')+1;
if(editColumn==statusCol){
temporizador(sheet,rowIndex,valor,tiempoActual,datestartCol,datendCol);
}
}
}
function temporizador(sheet,rowIndex,valor,tiempoActual,datestartCol,datendCol){
if (valor=='IN PROGRESS'){
sheet.getRange(rowIndex,datestartCol).setValue(Utilities.formatDate(tiempoActual,'GMT-3','dd/MM/yyyy'));
}
if(valor=='DONE'){
sheet.getRange(rowIndex,datendCol).setValue(Utilities.formatDate(tiempoActual,'GMT-3','dd/MM/yyyy'));
}
}
Here's an Example Sheet where I need timestamps.
Thanks!

Related

Script eror message

I have a big query that I am using in google scripts and I can't make it past the following as I keep getting the error.
TypeError: Cannot read property 'makeCopy' of undefined
createReport # Report Automation - Sent via Slack.gs:13
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Attribution Extract");
var lr = sheet.getLastRow();
var lc = sheet.getLastColumn();
var sourceData = sheet.getRange(1, 1, lr, lc).getValues();
var timestamp = Utilities.formatDate(new Date(), "America/New_York", "M/d");
for (row in sourceData) {
if (sourceData[row][12] === "RunNow" && sourceData[row][7] !== "")
var templateId = DriveApp.getFileById("1CLzYIh6UqNUrJW5lz9DENoiopOqhQpVLd-XpZ9otzg0");
var copy = templateId.makeCopy();
var newId = copy.getId()
var newSheet = SpreadsheetApp.openById(newId);
var rowNum = parseInt(row);
var opp = sourceData[row][7];
var AMslackID = sourceData[row][14];```

Create a Sub sheet from Master Sheet, using import range, filter and match Google App Script

I want to fetch data from a google sheet data dump/master sheet. There is a sheet for each month. Import/match the data to columns that are in a different order in another sub spreadsheet and put all months on one sheet. Lastly, I want to filter the data by person, status and date, so only these items will show unless Cell H1 is populated. If cell H1 is populated and not null, all data that meets the criteria will be shown.
Master: https://docs.google.com/spreadsheets/d/1nIzCqQUL1K4HwYUGV0jxjJjLKSFaeqVn27f_pkoL9j0/edit?usp=sharing
Subsheet:https://docs.google.com/spreadsheets/d/1-V0F_pJTKFmP8jRBwpIzBe_sP7GbC-314oUB97N0K-0/edit#gid=0
function Importmatchfilter() {
// source sheet
var ss = ('1nIzCqQUL1K4HwYUGV0jxjJjLKSFaeqVn27f_pkoL9j0/edit#gid=1395833187');
var ssn1 = ss.getSheetByName('April'); ('A:Z')
var ssn2 = ss.getSheetByName('May'); ('A:Z')
var ssn3 = ss.getSheetByName('June'); ('A:Z')
var ssn4 = ss.getSheetByName('July'); ('A:Z')
var ssn5 = ss.getSheetByName('August'); ('A:Z')
var ssn6 = ss.getSheetByName('September'); ('A:Z')
var ssn7 = ss.getSheetByName('October'); ('A:Z')
var ssn8 = ss.getSheetByName('November'); ('A:Z')
var ssn9 = ss.getSheetByName('December'); ('A:Z')
// Get full range of data
var SRange1 = ssn1.getDataRange();
var SRange2 = ssn2.getDataRange();
var SRange3 = ssn3.getDataRange();
var SRange4 = ssn4.getDataRange();
var SRange5 = ssn5.getDataRange();
var SRange6 = ssn6.getDataRange();
var SRange7 = ssn7.getDataRange();
var SRange8 = ssn8.getDataRange();
var SRange9 = ssn9.getDataRange();
// get A1 notation identifying the range
var A1Range = SRange1.getA1Notation();
var A1Range1 = SRange1.getA1Notation();
var A1Range2 = SRange2.getA1Notation();
var A1Range3 = SRange3.getA1Notation();
var A1Range4 = SRange4.getA1Notation();
var A1Range5 = SRange5.getA1Notation();
var A1Range6 = SRange6.getA1Notation();
var A1Range7 = SRange7.getA1Notation();
var A1Range8 = SRange8.getA1Notation();
var A1Range9 = SRange9.getA1Notation();
// get the data values in range
var SData1 = SRange1.getValues();
var SData2 = SRange2.getValues();
var SData3 = SRange3.getValues();
var SData4 = SRange4.getValues();
var SData5 = SRange5.getValues();
var SData6 = SRange6.getValues();
var SData7 = SRange7.getValues();
var SData8 = SRange8.getValues();
var SData9 = SRange9.getValues();
// target sheet
var ts = tss.getSheetByName('Sheet1');
function listMajors() {
gapi.client.sheets.spreadsheets.values.get({
spreadsheetId: '1-V0F_pJTKFmP8jRBwpIzBe_sP7GbC-314oUB97N0K-0',
range: A1
}).then(function(response) {
var range = response.result;
if (range.values.length > 0) {
appendPre('Due date, Status, Address:');
for (i = 0; i < range.values.length; i++) {
var row = range.values[i];
appendPre(row[0] + ', ' + row[4]);
function getheaderValues(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Sheet2');
var range = sheet.getRange("A:Z");
var allOfColumnAz = range.getValues()
for (var i = 1; i < 10 ; i++){
var sData = eval("SData"+i);
var lastRow = ts.getLastRow();
var numRows = sData.length;
var numCols = sData[0].length;
ts.getRange(lastRow+(lastRow > 0 ? 2 : 1), 1, numRows, numCols).setValues(sData);
}}

Google Spreadsheet Permissions

I think it's for experts. There it is, step by step:
I created a Google Spreadsheet
The following script is running on it:
/*Updated and maintain by internetgeeks.org*/
function onEdit(event)
{
var timezone = "GMT-3"; var timestamp_format = "dd-MM HH:mm"; // Timestamp Format.
if (updateColName = "Nº Cotação") {
var sheet = event.source.getSheetByName('Cotações'); //Name of the sheet where you want to run this script.
var updateColName = "Nº Cotação"; var timeStampColName = "Data proposta";
var actRng = event.source.getActiveRange();
var editColumn = actRng.getColumn();
var index = actRng.getRowIndex();
var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues();
var dateCol = headers[0].indexOf(timeStampColName); var updateCol = headers[0].indexOf(updateColName); updateCol = updateCol+1;
if (dateCol > -1 && index > 1 && editColumn == updateCol) { // only timestamp if 'Last Updated' header exists, but not in the header row itself!
var cell = sheet.getRange(index, dateCol + 1);
var date = Utilities.formatDate(new Date(), timezone, timestamp_format); cell.setValue(date);}
if (updateColName = "Resposta ao Cliente") {
var sheet = event.source.getSheetByName('Cotações'); //Name of the sheet where you want to run this script.
var updateColName = "Resposta ao Cliente"; var timeStampColName = "Contatado";
var actRng = event.source.getActiveRange();
var editColumn = actRng.getColumn();
var index = actRng.getRowIndex();
var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues();
var dateCol = headers[0].indexOf(timeStampColName); var updateCol = headers[0].indexOf(updateColName); updateCol = updateCol+1;
if (dateCol > -1 && index > 1 && editColumn == updateCol) { // only timestamp if 'Last Updated' header exists, but not in the header row itself!
var cell = sheet.getRange(index, dateCol + 1);
var date = Utilities.formatDate(new Date(), timezone, timestamp_format); cell.setValue(date);}
}
}
}
This spreadsheet is shared with other users, as editors
Some intervals are restrict for some editors
THE ISSUE
When the Owner updates the two described columns (see the above code), the script works fine
When shared editors modify something, just the second condition (if) of the script is working
Can someone help me on this?
Is there a problem with the script, maybe? Why does it works properly just on the spreadsheet owner?
Instead of using single '='
Use double '=='
Change the following statement in your code:
if (updateColName = "Nº Cotação")
To:
if (updateColName == "Nº Cotação")
and also change the following:
if (updateColName = "Resposta ao Cliente")
To:
if (updateColName == "Resposta ao Cliente")
UPDATE:
Here is the complete script code which should work as per your requirement:
function onEdit(event) {
var actRng = event.source.getActiveRange();
var actSheet = actRng.getSheet();
if( actSheet.getName() == 'Cotações' && actRng.getRowIndex() > 1 ) {
var updateColName = actSheet.getRange(1,actRng.getColumn()).getValue();
var headers = actSheet.getRange(1, 1, 1, actSheet.getLastColumn()).getValues();
var date = Utilities.formatDate(new Date(),'GMT-3','dd-MM HH:mm');
if( updateColName == 'Nº Cotação' ) {
var dateCol = headers[0].indexOf('Data proposta');
actSheet.getRange(actRng.getRowIndex(),dateCol+1).setValue(date);
}
else if( updateColName == 'Resposta ao Cliente' ) {
var dateCol = headers[0].indexOf('Contatado');
actSheet.getRange(actRng.getRowIndex(),dateCol+1).setValue(date);
}
}
};

Passing a value to other sheet

I have a value in my sheet where I want to pass the value to other sheet using Google App Script, I have a simple code that can get the value but I don't know on how I can pass it to other sheet and create a new row in other sheet.
function doit() {
var ss = SpreadsheetApp.getActiveSpreadsheet()
var FromSheet = ss.getSheetByName('s1');
var ToSheet = ss.getSheetByName('s2');
var firstname = fromsheet.getRange('A2');
var lastname = fromsheet.getRange('B2');
var data = firstname.getValue();
var data1 = lastname.getValue();
//To sheet code???
}
You can simplify a bit the code and it i almost same code to paste value.
function doit() {
var ss = SpreadsheetApp.getActiveSpreadsheet()
var FromSheet = ss.getSheetByName('s1');
var ToSheet = ss.getSheetByName('s2');
var firstname = fromsheet.getRange('A2').getValue();
var lastname = fromsheet.getRange('B2').getValue();
//Copy to others sheet
ToSheet.getRange('A2').setValue(firstname);
ToSheet.getRange('A2').setValue(firstname);
}
You can also copy the data with copyValuesToRange()
see : https://developers.google.com/apps-script/reference/spreadsheet/range#copyvaluestorangegridid-column-columnend-row-rowend
code example from site :
var ss = SpreadsheetApp.getActiveSpreadsheet();
var FromSheet = ss.getSheetByName('s1');
var ToSheet = ss.getSheetByName('s2');
var range = FromSheet.getRange("A2:B2");
range.copyTo(ToSheet.getRange("A2"), {contentsOnly:true});
Stéphane
Your code has a couple errors in it:
function doit() {
var ss = SpreadsheetApp.getActiveSpreadsheet() // missing ;
var FromSheet = ss.getSheetByName('s1');
var ToSheet = ss.getSheetByName('s2');
var firstname = fromsheet.getRange('A2'); // fromsheet must be FromSheet (Caps Matter)
var lastname = fromsheet.getRange('B2'); // same here
var data = firstname.getValue();
var data1 = lastname.getValue();
//To sheet code???
}
Here's what I would do:
function doit() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var FromSheet = ss.getSheetByName('s1');
var ToSheet = ss.getSheetByName('s2');
var firstname = FromSheet.getRange('A2').getValue(); // simplify lines
var lastname = FromSheet.getRange('B2').getValue(); // simplify lines
ToSheet.getRange("CELL TO PUT FIRSTNAME").setValue(firstname);
ToSheet.getRange("CELL TO PUT LASTNAME").setValue(lastname);
}

Do a function based on a value in a variabel Google Apps script calendar/spreadsheet

My script collects data from a Google Form. And puts it into a Spreadsheet. Based on the info in the spreadsheet it should add Calendar events. When the user chose 1 of 2 countrys in the form i would like the script to execute the function for that country.
Norway is one value
Sweden is one value
For example if the value in var country2 = country1.getValue(); is "Sweden" i would like it to only execute the function sweButik(){ Is it possible, and how do i do it?
function getData() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
var lastRow = sheet.getLastRow();
var lastColumn = sheet.getLastColumn();
var lastCell = sheet.getRange(lastRow, lastColumn);
var time1 = sheet.getRange(lastRow, 3);
var name1 = sheet.getRange(lastRow, 4);
var info1 = sheet.getRange(lastRow, 5);
var country1 = sheet.getRange(lastRow, 6);
var time2 = time1.getValue();
var name2 = name1.getValue();
var info2 = info1.getValue();
var country2 = country1.getValue();
}
//////////////////////////
function sweButik(){
var sweButik1 = CalendarApp.getCalendarById('company.com_7gvpuutc6h1d7kk5mjb2s5f4ds#group.calendar.google.com').createEvent(name2,
new Date(time2),
new Date(time2), {description: info2});
var sweButik2 = CalendarApp.getCalendarById('company.com_bertj0lfja45425ran8mmrpgc8#group.calendar.google.com').createEvent(name2,
new Date(time2),
new Date(time2), {description: info2});
}
/////////////////////////
function norButik(){
var norButik1 = CalendarApp.getCalendarById('company.com_7d88i0ne013r26i172k8fu16j0#group.calendar.google.com').createEvent(name2,
new Date(tid2),
new Date(tid2), {description: info2});
var norButik2 = CalendarApp.getCalendarById('company.com_sb0ddqmj5ql6uu5769u0qfs2go#group.calendar.google.com').createEvent(name2,
new Date(time2),
new Date(time2), {description: info2});
}
Since your values are fixed, you can do something like :
var country2 = country1.getValue();
if (country2 == "Sweden") {
sweButik();
} else if (country2 == "Norway") {
norButik();
}
But you need some parameters to be passed to these functions, so sweButik will become :
function sweButik (name,time,info) {
var sweButik1 = CalendarApp.getCalendarById('company.com_7gvpuutc6h1d7kk5mjb2s5f4ds#group.calendar.google.com').createEvent(name,
new Date(time),
new Date(time), {description: info});
var sweButik2 = CalendarApp.getCalendarById('company.com_bertj0lfja45425ran8mmrpgc8#group.calendar.google.com').createEvent(name,
new Date(time),
new Date(time), {description: info});
}
And its call will be :
sweButik(name2, time2, info2);
Don't forget to do the same for norButik

Categories

Resources