Passing a value to other sheet - javascript

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);
}

Related

i want to copy data from one sheet another sheet only select rows with checkbox In Google App Script

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);
}

Color Referencing a Related Cell

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");
}

TypeError: Cannot read property "values" from undefined. onEdit # ClosedTSR.gs:3

// When Form Gets edited
function onEdit(e) {
var Timestamp = e.values[2];
var email_sender = e.values[3];
var email_address = "";
var TSRNumber = e.values[19];
var IssueType = e.values[4];
var Region = e.values[0];
var Customer = e.values[5];
var ShortName = e.values[6];
var City = e.values[9];
var State = e.values[10];
var Contact = e.values[11];
var Product = e.values[12];
var TankCode = e.values[13];
var ProbDesc = e.values[14];
var Terminal = e.values[1];
var Priority = e.values[18];
var TSRStatus = e.values[22];
// Check if TSR is Closed
if(TSRStatus == "Closed"){
switch (Region) {
case "East":
email_address = xxxxxxxx
Basically when a user changes a cell value to "Closed" I am trying to send an email containing information from that specific row.
There is no values array in an onEdit trigger event object. There is for an onFormSubmit trigger.
event objects
I recommend reading before coding or at least during coding

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);
}}

error in assigning values to google spreadsheet cell

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);
}

Categories

Resources