New to coding. I am attempting to activate a color in a cell two rows below the current cell and am having difficulty making it work. Here is what I have so far. Any help is greatly appreciated!
function relatedCellColor() {
var spreadSheet = SpreadsheetApp.getActiveSpreadsheet();
var dataSheet = spreadSheet.getSheetByName("Data");
var currentCell = dataSheet.getCurrentCell();
var relatedCell = currentCell.offset(0,-2).getValue();
var defColor = dataSheet.getRange("DefaultColorCell").getBackgroundColor();
relatedCell.setBackground(defColor);
}
Thank you all! This is the code that worked:
function relatedCellColor() {
var spreadSheet = SpreadsheetApp.getActiveSpreadsheet();
var dataSheet = spreadSheet.getSheetByName("Data");
var defColor = dataSheet.getRange("DefaultColorCell").getBackgroundColor();
dataSheet.getCurrentCell().offset(2,0).setBackground(defColor);
}
function relatedCellColor() {
var ss = SpreadsheetApp.getActive();
var sh = ss.getSheetByName("Data");
sh.getCurrentCell().offset(2,0).setBackground("DefaultColorCell");
}
Related
I want to copy srange data in drange on check box validation. below only print data in logger.log but not in drange.
function insertrow(){
var spreadsheet = SpreadsheetApp.getActive();
var full = spreadsheet.getSheetByName("Source");
var shed = spreadsheet.getSheetByName("Destination");
var lr = full.getLastRow();
var srange = full.getRange(2,2,lr,2).getValues();
var dlr = shed.getLastRow();
var drange = shed.getRange(dlr+1,1);
srange.forEach (function(row){
if(row[0]){
Logger.log((JSON.stringify(row)));
}
})
}
Try
function insertrow() {
var spreadsheet = SpreadsheetApp.getActive();
var full = spreadsheet.getSheetByName("Source");
var shed = spreadsheet.getSheetByName("Destination");
var lr = full.getLastRow();
var srange = full.getRange(2, 1, lr, 3).getValues().filter(r => r[0]==true);
var dlr = shed.getLastRow();
shed.getRange(dlr + 1, 1,srange.length,3).setValues(srange);
}
Used below script it is returning null output
enter image description here
I got it to work, try this:
function Cpy_cmt(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
goToSheet('Master_Collection');
var activeCell = sheet.getRange("AE13")
var currentCellValue = activeCell.getValue();
var currentCellNote = activeCell.getNote();
}
function goToSheet(sheetName) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
ss.setActiveSheet(ss.getSheetByName(sheetName));
}
I need someone to modify this script that I found here to update or replace the row value of the sheet2 from sheet1 when a unique ID is found. This only works up to the 2nd column..I need something to update or replace rows in the sheet2 up to 10 columns or more. I have a sample spreadsheet. If you see on sheet2, some row values are missing or not yet updated, so I need those rows to be updated or replaced what's in sheet1.
Here is the spreadsheet:
https://docs.google.com/spreadsheets/d/100sjGCr0HSdJE1AERCJ9bGKiu3Vs2KxrvGIIjIrOjHw/edit?usp=sharing
Thanks for helping and sharing your knowledge.
function updateEntrees() {
var ss=SpreadsheetApp.getActive();
var sh1=ss.getSheetByName('Sheet1');
var rg1a=sh1.getRange(2,1,sh1.getLastRow()-1,1);
var vA1a=rg1a.getValues();
var rg1b=sh1.getRange(2,2,sh1.getLastRow()-1,1);
var vA1b=rg1b.getValues();
var sh2=ss.getSheetByName('Sheet2');
var rg2a=sh2.getRange(2,1,sh2.getLastRow()-1,1);
var vA2a=rg2a.getValues();
var rg2b=sh2.getRange(2,2,sh2.getLastRow()-1,1);
var vA2b=rg2b.getValues();
for(var i=0;i<vA1a.length;i++) {
for(var j=0;j<vA2a.length;j++) {
if(vA1a[i][0]==vA2a[j][0]) {
vA2b[j][0]=vA1b[i][0]
}
}
}
rg2b.setValues(vA2b);
Try the following code, in the second line, change the value of "numColumns" as per your need:
function updateEntrees() {
var numColumns = 9;
var ss = SpreadsheetApp.getActive();
var sh1 = ss.getSheetByName('Sheet1');
var rg1a = sh1.getRange(2,1,sh1.getLastRow()-1,1);
var vA1a = rg1a.getValues();
var rg1b = sh1.getRange(2,2,sh1.getLastRow()-1,numColumns);
var vA1b = rg1b.getValues();
var sh2 = ss.getSheetByName('Sheet2');
var rg2a = sh2.getRange(2,1,sh2.getLastRow()-1,1);
var vA2a = rg2a.getValues();
var rg2b = sh2.getRange(2,2,sh2.getLastRow()-1,numColumns);
var vA2b = rg2b.getValues();
for( var i=0; i < vA1a.length; i++ ) {
for( var j=0; j < vA2a.length; j++ ) {
if( vA1a[i][0] == vA2a[j][0] ) {
vA2b[j] = vA1b[i];
}
}
}
rg2b.setValues(vA2b);
};
I am new to google script. I want to assign a value to variable from spreadsheet cell and after doing arithmetic operations, want to assign it to particular cell in spreadsheet.
I am using following code :
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sh = ss.getSheetByName("PATTERNS");
var dd = sh.getRange("C4").getValue();
var ee = sh.getRange("D4").getValue();
var x = dd - ee;
sh.getRange("P4").setValue(x);
}
however, value at cell P4 is showing error : #NUM!
please help!
also i want to use for loop for cell no such as
for (var i = 1, i <60, i++)
{
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sh = ss.getSheetByName("PATTERNS");
var dd = sh.getRange("C[i]").getValue();
var ee = sh.getRange("D[i]").getValue();
var x = dd - ee;
sh.getRange("P[i]").setValue(x);
}
How to get this? Can someone please help me?
Try this:
function myFunction() {
var ss=SpreadsheetApp.getActiveSpreadsheet();
var sh=ss.getSheetByName("PATTERNS");
var rg=sh.getDataRange();
var vA=rg.getValues();
var change=false;
for(var i=0;i<vA.length;i++)
{
if(!isNan(vA[i][2]) && !isNan(vA[i][3]))
{
vA[i][15]=vA[i][2]-vA[i][3];
change=true;
}
}
if(change)
{
rg.setValues(vA);
}
}
Try this code
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sh = ss.getSheetByName("PATTERNS");
var dd = sh.getRange("C4").getValue();
var ee = sh.getRange("D4").getValue();
var x = parseInt(dd) - parseInt(ee);
sh.getRange("P4").setValue(x);
}
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);
}