Google Sheet SetValu of cell which is 2 cells ahead - google-apps-script

I am trying to set the value in the ebayFee cell once i enter a sold value in the sold cell.
Also, is this possible to make this function automatically so it runs every time on a new row when i enter a value to sold it automatically populate the rest for me.
function onEnter(num) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var row = ss.getActiveCell().getRow();
var ebayfee = ( num * 8 ) / 100;
var cell = ss.getRange('E' + row).setValue(ebayfee);
}

Try the following code, change the sheet name "Sheet1" as per your need:
function onEdit(e) {
let r = e.range;
if( r.getSheet().getName() === 'Sheet1' ) {
if( r.rowStart >= 2 && r.columnStart === 3 ) {
r.offset( 0, 2 ).setValue( r.getValue() * 8 / 100 );
}
}
};

Related

scrips to subtract value in one cell from another cell

My current script adds a timestamp, i now want to add the function to subtract the value in cell 13 from that in cell 14 and put in into cell 12 at the same time it adds the date stamp
function onEdit(e) {
var row = e.range.getRow();
var col = e.range.getColumn();
var value = e.range.getValue();
var sheet = e.source.getActiveSheet().getName();
var destination = e.source.getActiveSheet().getRange(row,11);
var destination2 = e.source.getActiveSheet().getRange(row,10);
var num1 = e.source.getActiveSheet().getRange(row,13).getValue;
var num2 = e.source.getActiveSheet().getRange(row,14).getValue;
if(col === 4 && row > 4 && sheet ==="Bayswater" && value === "Completed" && destination2.getValue() ==="" && destination.getValue() ===""){
e.source.getActiveSheet().getRange(row,11).setValue(new Date(new Date().setHours(0,0,0,0))).setNumberFormat('dd-MMM-yy');
e.source.getActiveSheet().getRange(row,10).setValue(new Date(new Date().setHours(0,0,0,0))).setNumberFormat('dd-MMM-yy');
e.source.getActiveSheet().getRange(row,12).setValue('=num1-num2')
}
Probably the last line should be like this:
e.source.getActiveSheet().getRange(row,12).setFormula('=(N' + row + '-M' + row + ')');
Or it was just a typo. You forgot to add () after getValue in the two lines:
var num1 = e.source.getActiveSheet().getRange(row,13).getValue(); // <-- here
var num2 = e.source.getActiveSheet().getRange(row,14).getValue(); // <-- here
If you add () you can use the last line this way:
e.source.getActiveSheet().getRange(row,12).setValue(num1-num2);

Is there a way to prevent spreadsheet from updating the date to today's date in formula?

I'm trying to print the date when I change the value on a cell (status column). But spreadsheets update the date to today's date. I wish it could save the date from when the status was changed.
The code to print the date:
function TIMESTAMP() {
var today = new Date();
var date = Utilities.formatDate(today, 'GMT-3', 'dd/MM/yyyy');
return date;
}
In the spreadsheet, the column D is where I set the status and columns H to J print the date from when the status was changed.
The code for column H is the following:
=ARRAYFORMULA(IF(ROW(H:H)=1;"Logística";IF(ISBLANK(D:D);;IF((D:D>=3)*(D:D<6);TIMESTAMP();IF(D:D<1;"Aguardando Pagamento";IF(D:D=2;"Aguardando Etiqueta";IF(D:D=6;"Cancelado";"Aguardando Faturamento")))))))
The codes for columns I and J are similar to H. So, how to print the date from when the status in column D was changed and keep spreadsheet from updating to today's date?
Not sure but how about this:
function onEdit(e) {
const sh=e.range.getSheet();
if(sh.getName() == 'Sheet Name' && e.range.columnStart == 4 ){
let dt = Utilities.formatDate(new Date(), 'GMT-3', 'dd/MM/yyyy');
e.range.offset(0,4).setValue(dt)
}
}
You have to change the sheet name
Since you manually change the state on column D, you may use an onEdit(e) trigger to override the formula with the value that you want:
function onEdit(e) {
const { range } = e
const sheet = range.getSheet()
if(sheet.getName() === 'Set your sheet name here' && range.getColumn() <= 4 && range.getLastColumn() >= 4){
const firstRow = range.getRow()
const values = sheet
.getRange(range.getRow(), 4, range.getHeight(), 1)
.getValues()
.flat()
for (let i = 0; i < values.length; i++) {
const value = values[i]
if (!Number.isInteger(value) || value < 3 || value > 5) continue
const row = firstRow + i
const col = value - 3 + 8
const targetRange = sheet.getRange(row, col)
const todayStr = Utilities.formatDate(new Date(), 'GMT', 'dd/MM/yyyy')
targetRange.setValue(todayStr)
}
}
}
This code works even when setting multiple rows at the same time. Remember to set the name of your sheet and the timezone you are in.
References
Utilities.formatDate(date, timeZone, format) (Google Apps Script reference)
Class Range (Google Apps Script reference)

Automatic timestamp when a cell is modified, but that clears when the cell is empty

I have this code and i am trying to add some code at the bottom saying that if the column is empty, the date column will clear.
function onEdit(e) {
var ss = SpreadsheetApp.getActiveSheet();
var r = ss.getActiveCell();
//1.Change 'Ark 1' to be matching your sheet name
if (r.getColumn() == 4 && ss.getName()=='Ark 1') { // 2. If Edit is done in column 4 (D) And sheet name is Ark 1 then:
var celladdress ='L'+ r.getRowIndex()
ss.getRange(celladdress).setValue(new Date()).setNumberFormat("dd/MM/yyyy hh:mm");}
};
L is the column i have my date timestamp in, while column 4 (D) is the column the modification is done.
You don't explain in your question either you mean the whole column being empty, or the active cell.
In any case:
To verify that the cell is not empty or contains only a space you have to query for if(r.getValue() !="" && r.getValue() !=" ")
Depending on the outcome - either you set a timestmap in the active row of column L or you clear the content of the respective cell in column L with ss.getRange(celladdress).clear();
Below is a sample:
function onEdit(e) {
var ss = SpreadsheetApp.getActiveSheet();
var r = ss.getActiveCell();
//1.Change 'Ark 1' to be matching your sheet name
if (r.getColumn() == 4 && ss.getName()=='Ark 1') { // 2. If Edit is done in column 4 (D) And sheet name is Ark 1 then:
var celladdress ='L'+ r.getRowIndex();
if(r.getValue() !="" && r.getValue() !=" "){
ss.getRange(celladdress).setValue(new Date()).setNumberFormat("dd/MM/yyyy hh:mm");
} else{
ss.getRange(celladdress).clear();
}
}
}

Automatic date if i change a cell

i know that's might a simple function, but i can't solve it.
I want to write a simple onEdit function with the following conditions:
If anything in a row (3 - xxx) is changed or updated, i want the actuel date in a specific column.
For example:
if I changed cell B10, I want the actuel date in cell G10. Also for an update in cell E10.
My head
function onEdit() {
var s = SpreadsheetApp.getActiveSheet;
var range = s.getActiveRange()
My loop over all rows is:
for(var i = 1; i<= range.getNumRows() ; i++){
var r = range.getCell(i, 1)
if( s.getName() == "Sheet1" ) {
My if-statement:
if( r.getColumn() == 2 ) {
var nextCell = r.offset(0, 2);
if( nextCell.getValue() === '' ) {//is empty?
var time = new Date();
time = Utilities.formatDate(time, "GMT+01:00", "HH:mm:ss MM/dd/yy");
nextCell.setValue(time);
}
};
};
I'll be happy if someone can helpme ;-)
function onEdit() {
var s = SpreadsheetApp.getActiveSheet();
// Instead of getting activeCell we get the whole range
var range = s.getActiveRange()
//Use For loop to go through each row and add the time Stamp
for(var i = 1; i<= range.getNumRows() ; i++){
var r = range.getCell(i, 1) //Assumption here is that data is pasted in one column only
// IF that is not always the case, you will have to get the range over which the data was pasted and select column 2
if( s.getName() == "Gesamt" ) { //checks that we're on the correct sheet
//var r = s.getActiveCell(); calling it again doesnt change its the value
// If it is first time keyword added, we will add the current date to "Date Added" Column
// We will if it is the first time the "Keyword" column has been written
for (var c = 1; c<=26; c++){
if( r.getColumn() == c) {
var k = 26 - c; //every updated time to the column 26 in the same row
var nextCell = r.offset(0, k);
//if( nextCell.getValue() === '' ) {//is empty?
var time = new Date();
time = Utilities.formatDate(time, "GMT+01:00", "HH:mm:ss MM/dd/yy");
nextCell.setValue(time);
};
};
}
}
}
There is no reason to loop on an on-edit trigger, as you will automatically be in the correct row and column every time there is an edit:
function onEdit(e){
var sheet = SpreadsheetApp.getActiveSheet();
var range = e.range; //gets edited range
var column = range.getColumn();
if (column < 3){ //stops if column is too low
return;
}
if (column > 6){ //stops is column is too high
return;
}
//else puts date in same row in column G
var row = range.getRow();
var time = new Date();
var stringTime = Utilities.formatDate(time, "GMT+01:00", "HH:mm:ss MM/dd/yy");
sheet.getRange(row, 7).setValue(stringTime);
}

How to Automatically Change a Cell in Google Sheets Based on Multiple Variables

I am working on creating a schedule in Google Sheets and I have just one last task that I cannot figure out. Here is an example of what I would like to accomplish.
For lines 8-12 (which is for Employees 1-5) I need to have a script that looks for every cell that contains either A or A-10. If these A or A-10 shifts fall on the 1-15 (days of the month are in row 5...pictured below) they should be changed to Aa or Aa-10. If they fall on the 16th-end of the month they should be Ap or Ap-10. The same will be the case with the R and I shifts.
Here is a screen shot of the Google Sheet from the example I gave above:
Any help would be greatly appreciated! Thanks!
Here is a script that will do those changes. It only changes rows 8 - 12 and assumes the dates are in row 5.
function changeValues() {
var ss = SpreadsheetApp.getActive();
var sheet = ss.getActiveSheet();
var range = sheet.getRange("8:12");
var dates = sheet.getRange("5:5").getValues();
var values = range.getValues();
for(var row = 0; row < values.length; row++) {
for(var column = 1; column < values[row].length; column++) {
var day = Number(dates[0][column].replace(/[^0-9]/g, ""));
var value = values[row][column];
if(day >= 1 && day <= 15) {
if(value === "A") value = "Aa";
else if(value === "A-10") value = "Aa-10";
} else if(day > 15) {
if(value === "A") value = "Ap";
else if(value === "A-10") value = "Ap-10";
}
values[row][column] = value;
}
}
range.setValues(values);
}