Im trying to bring the first 7 column from a spreadsheet to another spreadsheet using google app script but I honestly searched a lot and didn't find a way to do so.
function MoveCode(){
var ss1 = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/1zU__ccPIMst54whmyrbmRnDRRjOtQBFPzXhw6NsFqpU/edit#gid=432949714");//369
var sheet1 = ss1.getSheetByName("Sourcing");
var wpLink1 = sheet1.getRange("A2:G").getValues();
var ssData1 = SpreadsheetApp.openByUrl("This-Spread-sheet");
var sheetPrime1 = ssData1.getSheetByName("Sheet1");
var data = sheet1.getRange("A2:G").getValues();
sheetPrime1.
}
I want to move the code in a way where if i update the first one it would be updated in the second one.
If you need more explanation please let me know.
Thank you.
Move first seven columns
function MoveCode(){
const ss1 = SpreadsheetApp.getActive();
const ss2 = SpreadsheetApp.openById("id");//or url whatever
const ssh = ss1.getSheetByName('Sourcing');
const dsh = ss2.getSheetByName('Sheet1');
const vs = ssh.getRange(2,1,ssh.getLastRow() - 1,7).getValues();
dsh.getRange(2,1,vs.length,vs[0].length).setValues(vs);
}
Expanding on Coopers example. I think what you want is everytime you change a value in sheet1 it will automatically update in sheet2. You could either use an onEdit limited to the first 7 columns of sheet1 to copy the value to sheet2. Or here is script that creates a forumla in each cell of sheet2 to the cell in sheet1. But you really only need to run this once unless you change the size of either spreadsheet.
function moveCode() {
try {
const ss1 = SpreadsheetApp.getActive();
//const ss2 = SpreadsheetApp.openById("id");//or url whatever
const ssh = ss1.getSheetByName('Sheet1'); // source
const dsh = ss2.getSheetByName('Sheet2'); // destination
var srange = ssh.getDataRange();
var formulas = [];
var i=0;
var j= 0;
var row = null;
// srange.getNumRows()-1 because skip the first row
for( i=0; i<srange.getNumRows()-1; i++ ) {
row = [];
for( j=1; j<8; j++ ) {
row.push("=Sheet1!"+String.fromCharCode(64+j)+(i+2).toString()); // A = 65
}
formulas.push(row);
}
dsh.getRange(2,1,formulas.length,formulas[0].length).setValues(formulas);
}
catch(err) {
Logger.log(err);
}
}
Related
I need to extract a data from this other sheet where the header will repeat itself by the number of items under it. So far I can only do this with the first column from the data source and I can’t seem to get the right code where the loop will go through the next column once it detects the first empty cell of the previous column.
Here’s where I am getting stuck
function filter(){
var sheet = SpreadsheetApp.getActiveSpreadsheet()
var data = sheet.getSheetByName("data")
var filter = sheet.getSheetByName("filter")
var dataRange = data.getDataRange().getValues();
var dataLr = data.getLastRow();
var dataLc = data.getLastColumn();
var row = 1
var col = 1
for(var i=1; i < dataRange.length; i++){
let targetAppName = filter.getRange(i+1,1)
let targetQname = filter.getRange(i+1,2)
targetQname.setValue(dataRange[i])
targetAppName.setValue(dataRange[0][0])
}
}
I’m trying to get these results in another sheet tab:
|Names | Subjects |
|:—————|:———————-:|
|Michael Lowry| Trigonometry|
|Michael Lowry| Biology|
And so on, until it reads all columns and rows of the data range
Loop throught each column of each row
function loopthroughteachcolumnofarow() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getActiveSheet();
const vs = sh.getDataRange().getValues();
vs.forEach((r,i) => {
r.forEach((c,j) => {
//c is the value for vs[i][j];
})
})
}
So I wrote this code that will paste the first 7 columns from sheet2(sourcing) to sheet1(sheet1) and I also wrote a formula to every cell so if the code in the sourcing sheet changes then the one in sheet1 would change, but when I did that I got #REF! in every cell so I went to file --> settings and changed the calculations --> Iterative calculation from Off to On and the error was gone but i got 0 in every cell which isnt the right value of the cell.
Here is the code.
function moveCode() {
try {
const ss1 = SpreadsheetApp.getActive();
const ss2 = SpreadsheetApp.openById("1A3tJiIEkDP_5R6mEY25IeAsO2XV_oveJwnUovxRd-1U");//or url whatever
const ssh = ss1.getSheetByName('Sheet1');
const dsh = ss2.getSheetByName('Sourcing');
const lastRow = dsh.getLastRow();
if (lastRow < 2) return;
var srange = dsh.getDataRange();
var formulas = [];
var i=0;
var j= 0;
var row = null;
// srange.getNumRows()-1 because skip the first row
for( i=0; i<srange.getNumRows()-1; i++ ) {
row = [];
for( j=1; j<8; j++ ) {
row.push("=Sheet1!"+String.fromCharCode(64+j)+(i+2).toString()); // A = 65
}
formulas.push(row);
}
ssh.getRange(2,1,formulas.length,formulas[0].length).setValues(formulas);
}
catch(err) {
Logger.log(err);
}
}
I think there is something wrong with the formula but I'm not sure whats wrong.
If you need more explanation please let me know.
Thank you.
In your situation, how about using IMPORTRANGE? When your script is modified, it becomes as follows.
Modified script:
function moveCode() {
try {
const ss1 = SpreadsheetApp.getActive();
const ss2 = SpreadsheetApp.openById("1A3tJiIEkDP_5R6mEY25IeAsO2XV_oveJwnUovxRd-1U");//or url whatever
const ssh = ss1.getSheetByName('Sheet1');
const dsh = ss2.getSheetByName('Sourcing');
const lastRow = dsh.getLastRow();
if (lastRow < 2) return;
ssh.getRange(2, 1).setFormula(`=IMPORTRANGE("${ss2.getUrl()}","'Sourcing'!A2:G${lastRow}")`);
}
catch (err) {
Logger.log(err);
}
}
After the formula was put in the cell, please accept for loading the values from the Spreadsheet.
References:
IMPORTRANGE
setFormula(formula)
I am trying to create a sheet (using Google Sheets) for our volunteers to search for, update, and/or add mentoring information (javascript).
I started with the option to search (function onSearch) and it appears to work but the information does not appear in the sheet (attached FYI). I'd appreciate help in making this run.
date entry sheet
REVISED:
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var formSS = ss.getSheetByName("Form1");
var str = formSS.getRange("D3").getValues()[3]; //Search for info entered in Form1$D3
var datasheet = ss.getSheetByName("TRACKING");
var values = datasheet.getRange(2,1,2); //Datasheet where info will be retrieved
if (values == str) {
var values1 = values.getValues(); //// get the tracking data if it matchs search request
var i = 1;
myFunction().onSearch = i < values.length; i++;
{
var output = datasheet.getRange(); ///retrieve information from the Tracking spreadsheet and
//populate the information in the appropiate cells.
formSS.get("E8").datasheet.getValue(1),
formSS.getRange("E10").getdatasheet.getValue(2),
formSS.getRange("E12").datasheet.getValue(3),
formSS.getRange("E14").datasheet.getValue(4),
formSS.getRange("J8").datasheet.getValue(5),
formSS.getRange("J10").datasheet.getValue(6),
formSS.getRange("J12").datasheet.getValue(7),
formSS.getRange("J14").datasheet.getValue(8);
return }}}
function onSearch() {
var SEARCH_COL_IDX=0;
var RETURN_COL_IDX=0;
var ss = SpreadsheetApp.getActiveSpreadsheet();
var formSS = ss.getSheetByName("Form1");
`` var datasheet = ss.getSheetByName("TRACKING");
var str = formSS.getRange("D3").getValues()[3]; //column Index
var values = ss.getSheetByName("Form1").getDataRange().getValues();
for (var i = 0; i < values.length; i++) {
var row = values[i];
if (row[SEARCH_COL_IDX] == str) {
RETURN_COL_IDX = i+1;
var values = [[formSS.getRange("E8").datasheet.getValue(1),
formSS.getRange("E10").getdatasheet.getValue(2),
formSS.getRange("E12").datasheet.setValue(3),
formSS.getRange("E14").datasheet.getValue(4),
formSS.getRange("J8").datasheet.getValue(5),
formSS.getRange("J10").datasheet.getValue(6),
formSS.getRange("J12").datasheet.getValue(7),
formSS.getRange("J14").datasheet.getValue(8)]];
}
}
}
Thanks for responding. No one had the answer, and I even read that what I was asking is not available in Google Sheets. I decided to use the filter function for each cell instead.
B3 is the search field TRACKING!C:C is the sheet to retrieve the
information Tracking!E:E is the matched column to return information.
I am new here and at programming but I hope this helps someone.
=IFERROR(FILTER(TRACKING!C:C,TRACKING!E:E=B3),TRUE)
I would like to create a simple google apps script to copy specific column into another sheets.
Previously I tried using getLastRow but I get stuck to modify it.
var destinationSheetLastRow = destinationSheet.getDataRange().getLastRow();
Here is my spreadsheet: https://docs.google.com/spreadsheets/d/1rGvmlKCmbjDSCLCC2Kujft5e4ngbSLzJd2NYu0sxISs/edit?usp=sharing
And here is the modified script so far:
function pasteMultiCol(sourceSheet, destinationSheet,sourceColumns,destinationColumns, doneColumn){
var sourceDataRange = sourceSheet.getDataRange();
var sourceDataValues = sourceDataRange.getValues();
var sourcesheetFirstRow = 0;
var sourceSheetLastRow = sourceDataRange.getLastRow();
var destinationSheetLastRow = destinationSheet.getDataRange().getLastRow();
var pendingCount = 0;
//Find the row start for copying
for(i = 0; i < sourceDataValues.length; i++){
if(sourceDataValues[i][doneColumn-1] === "Copied"){
sourcesheetFirstRow++;
};
if(sourceDataValues[i][doneColumn-1] === ""){
pendingCount++;
};
};
//Update Source sheet first row to take into account the header
var header = sourceSheetLastRow-(sourcesheetFirstRow + pendingCount);
sourcesheetFirstRow = sourcesheetFirstRow+header;
// if the first row equals the last row then there is no data to paste.
if(sourcesheetFirstRow === sourceSheetLastRow){return};
var sourceSheetRowLength = sourceSheetLastRow - sourcesheetFirstRow;
//Iterate through each column
for(i = 0; i < destinationColumns.length; i++){
var destinationRange = destinationSheet.getRange(destinationSheetLastRow+1,
destinationColumns[i],
sourceSheetRowLength,
1);
var sourceValues = sourceDataValues.slice(sourcesheetFirstRow-1,sourceSheetLastRow);
var columnValues =[]
for(j = header; j < sourceValues.length; j++){
columnValues.push([sourceValues[j][sourceColumns[i]-1]]);
};
destinationRange.setValues(columnValues);
};
//Change Source Sheet to Copied.
var copiedArray =[];
for(i=0; i<sourceSheetRowLength; i++){copiedArray.push(["Copied"])};
var copiedRange = sourceSheet.getRange(sourcesheetFirstRow+1,doneColumn,sourceSheetRowLength,1)
copiedRange.setValues(copiedArray);
};
function runsies(){
var ss = SpreadsheetApp.openById("1snMyf8YZZ0cGlbMIvZY-fAXrI_dJpPbl7rKcYCkPDpk");
var source = ss.getSheetByName("Source");
var destination = ss.getSheetByName("Destination");
var sourceCols = [4,5,6,7,8,9,10];
var destinationCols = [7,8,9,10,11,12,13];
var doneCol = 12
//Run our copy and append function
pasteMultiCol(source,destination, sourceCols, destinationCols, doneCol);
};
Your code is taken from my tutorial in my blog article Copy Selected Columns in One Sheet and Add Them To The Bottom of Different Selected Columns in Another Sheet and it just needs a tweak.
I think the issue might be that you have a bunch of formulas in other columns in your "Destination" Sheet tab. So getting the last row of the sheet will result in getting the last row considering all the data including your other formulas.
You might find this explanation in a follow up blog article I wrote helpful: Google Apps Script: Get the last row of a data range when other columns have content like hidden formulas and check boxes
In short, you can change the destinationSheetLastRow variable to something simple like this.
var destinationSheetLastRow = (()=>{
let destinationSheetFirstRow = 7; // The first row of data after your header.
//Get a sample range to find the last value in the paste columns.
let sampleRange = destinationSheet.getRange(destinationSheetFirstRow,
destinationColumns[0],
destinationSheet.getLastRow())
.getValues();
let sampleLastRow = 0;
while(sampleLastRow < sampleRange.length){
if (sampleRange[sampleLastRow][0] == ""){
break;
}
sampleLastRow++;
};
return sampleLastRow;
})()
I have used the example code from this link, to make the code below.
https://yagisanatode.com/2017/12/13/google-apps-script-iterating-through-ranges-in-sheets-the-right-and-wrong-way/
Most of the code is working as expected, except for the last row.
In Column E, I want to place the custom function =apiav() with the data from cell A.
However the code is returning =apiav(Range) in the Google sheet cells. (to be clear it should be something like =apiav(a1))
I tried everything i could think of and of course googled for hours, but i am really lost and can't find the right solution for this.
function energy(){
var sector = "Energy";
var ss=SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var rangeData = sheet.getDataRange();
var lastColumn = 2;
var lastRow = 999 ;
var sheet = ss.getSheets()[0];
var searchRange = sheet.getRange(2,2, lastRow-1 ,1 );
var ouputrange = sheet.getRange(2,4, lastRow-1 ,1 );
//clear range first
ouputrange.clear("D:D");
ouputrange.clear("E:E");
/*
GOOD - Create a client-side array of the relevant data
*/
// Get array of values in the search Range
var rangeValues = searchRange.getValues();
// Loop through array and if condition met, add relevant
// background color.
for ( i = 0; i < lastColumn - 1; i++){
for ( j = 0 ; j < lastRow - 1; j++){
if(rangeValues[j][i] === sector){
sheet.getRange(j+2,i+4).setValue("yes");
var formularange = sheet.getRange (j+2,i+1);
sheet.getRange(j+2,i+5).setValue('=apiav(' + formularange + ')');
}
};
};
};
Replace:
var formularange = sheet.getRange(j+2,i+1);
with:
var formularange = sheet.getRange(j+2,i+1).getA1Notation();
So you will be able to pass the cell reference instead of the range object.