Import multiple Spreadsheets into 1 Sheet using AppScript - javascript

I have a script that imports data from different google sheets into one sheet, it works perfectly except the fact that every time when I'm running the script the imported data starts further down in the sheet. What do I need to change in the script that every time i start the script it will push the data starting the first row of the sheet?
function getData() {
get_IDs=["1pkcy4jx14odSN-Ir2Nqai89_0dJc7RiLapWxRWIURj4","1J8_cZsjEa9c8vktPBvqGvcdwUBAVkWT3NSdTnXp6tMk"];
var ss=SpreadsheetApp.getActiveSpreadsheet();
var copySheet =ss.getSheetByName("Sheet3");
copySheet.getRange('A1:Z').clear();
for(z=0;z<get_IDs.length;z++)
{
var ss1=SpreadsheetApp.openById(get_IDs[z]);
var sheets =ss1.getSheetByName("Export Department");
var sheetsRange = sheets.getDataRange();
var sheetsValues = sheetsRange.getValues();
for(var y = 1;y<sheetsValues.length;y++)
{
copySheet.appendRow(sheetsValues[y]);
}
}
}```

Use setValues() instead of appendRow() and increment a counter by the number of rows on each import:
function getData() {
get_IDs = ["1pkcy4jx14odSN-Ir2Nqai89_0dJc7RiLapWxRWIURj4", "1J8_cZsjEa9c8vktPBvqGvcdwUBAVkWT3NSdTnXp6tMk"]
var ss = SpreadsheetApp.getActiveSpreadsheet()
var copySheet = ss.getSheetByName("Sheet3")
copySheet.getRange('A1:Z').clear()
let pasteRow = 1
for (z = 0; z < get_IDs.length; z++) {
var ss1 = SpreadsheetApp.openById(get_IDs[z])
var sheets = ss1.getSheetByName("Export Department")
var sheetsRange = sheets.getDataRange()
var sheetsValues = sheetsRange.getValues()
sheetsValues.splice(0, 1)
copySheet.getRange(pasteRow, 1, sheetsValues.length, sheetsValues[0].length).setValues(sheetsValues)
pasteRow += sheetsValues.length
}
}

Related

How to get some data from a spread sheet to another

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

Search and return data using getRange() and getValue

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)

Google scripts: How to put a range in setValue

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.

I need to redefine script on Google Sheets

I need help with this script. Basically is working, but running on the same ranges for the whole sheets ("A4:V50") and I need it to run different ranges in each sheet.
Thanks!
function clearIPs() {
var ss = SpreadsheetApp.getActive();
var allsheets = ss.getSheets();
for (var i = 0; i < 46; i++) {
var sheet = allsheets[i]
sheet.getRange("A4:V50").clearContent();
}
}
Create some type of Settings Objects. For example:
var Settings = {
Sheet1 : "A4:V50",
Sheet2 : "A4:B20"
}
And then in your code you can use this information like so:
for (var i = 0; i < 46; i++) {
var sheet = allsheets[i];
// lets get the right settings for that sheet
var sheetNumber = i + 1;
var range = Settings["Sheet" + sheetNumber];
// if there is no value for that sheet, lets add a default
if( !range ){
range = "A1:B20";
}
// now we can use that reference
sheet.getRange( range ).clearContent();
}

Copying all the sheets in every workbook within a google drive folder

I Want a google script that will loop through every workbook(spreadsheet) within my google drive folder, then copying every sheet within to one sheet of my own. I already have a script but its set to copy only the first sheet, could we make it loop to copy all sheets in the spreadsheet
I've tried to change the indexing but it'll only pull one sheet
function getDataToMaster() {
var folder = DriveApp.getFolderById("******************"); //Define id of folder
var contents = folder.getFiles();
var file;
var data;
var sheetMaster = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0]; //first sheet of the file, change by getSheetByName("NAME") if you want
while(contents.hasNext()){
file = contents.next();
if (file.getMimeType() == "application/vnd.google-apps.spreadsheet") {
var sheet = SpreadsheetApp.openById(file.getId()).getSheets()[0];//first sheet of the file, change by getSheetByName("NAME") if you want
var startRow = 1;
var data = sheet.getDataRange().getValues();
var colToCheck = 7;
for(var j = 0; j < data.length;j++){
if(data[j][colToCheck-1] != "copied"){
sheetMaster.appendRow(data[j]);
sheet.getRange((j+1), colToCheck).setValue("copied");
SpreadsheetApp.flush();
}
}
}
}
}
Spreadsheet.getSheets() returns an array of Sheets you can iterate over.
Suggestion: when you know you will iterate through each element in an array (i.e. you don't want to exit early through break or return to terminate a loop), then try Array.prototype.forEach, as it reduces the amount of variables you need to setup. The following is how I would rewrite your getDataToMaster function using forEach twice:
function getDataToMaster() {
var folder = DriveApp.getFolderById("******************");
var contents = folder.getFiles();
var sheetMaster = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];
while (contents.hasNext()) {
var file = contents.next();
if (file.getMimeType() !== "application/vnd.google-apps.spreadsheet") {
continue;
}
SpreadsheetApp.openById(file.getId()).getSheets()
.forEach(function (sheet) {
var colToCheck = 7;
sheet.getDataRange().getValues()
.forEach(function (row, index) {
if (row[colToCheck - 1] != "copied") {
sheetMaster.appendRow(row);
sheet.getRange(index + 1, colToCheck).setValue("copied");
SpreadsheetApp.flush();
}
});
});
}
}
It should work if you add a for loop underneath of your if statement like so
if (file.getMimeType() == "application/vnd.google-apps.spreadsheet") {
var sheetCount = SpreadsheetApp.openById(file.getId()).getNumSheets();
for(var k = 0; k < sheetCount; k++){
var sheet = SpreadsheetApp.openById(file.getId()).getSheets()[k];//iterates through each sheet in the file
var startRow = 1;
var data = sheet.getDataRange().getValues();
var colToCheck = 7;
for(var j = 0; j < data.length;j++){
if(data[j][colToCheck-1] != "copied"){
sheetMaster.appendRow(data[j]);
sheet.getRange((j+1), colToCheck).setValue("copied");
SpreadsheetApp.flush();
}
}
}
}
How getSheets()[Integer] works is that it gets the sheet with the matching index number as supplied. The first sheet in the spreadsheet is index 0, the second is 1, third is 2, and so on. This loop just gets how many sheets there are and iterates through the indexes performing the actions you wrote for each individual sheet.

Categories

Resources