Getting Specific Rows from Another Google Sheet via App Script Based on Multiple Cell Values - google-apps-script

I would like to fetch data from another google sheet, then retrieve all columns(been successful with this one). As for the rows, I would only like to retrieve all rows with these values, "Mrm1, Mrm2, Mrm3" from Measurement Column.
For example, I have these on another sheet,Original Data
And on another sheet, I want the result to be like this: Expected Retrieved Data
So far, I have this, but I'm not sure how to apply what I need on the rows. This script is retrieving everything.
function Country A() {
var sss = SpreadsheetApp.openById("19-idD8PWuNxnvzRVqBlKhju2RkKxOe8nUQaf9ZMAcgk"); // Source spreadsheet - replace with actual key
var ss = sss.getSheetByName('Compiled'); // Source sheet - change to actual name
var numRows = ss.getLastRow() // get last row with data
var numColumn = ss.getLastColumn(); // get last column with data
var srange = ss.getRange(2,1,numRows,numColumn); // Source range - change to actual range
var values = srange.getDisplayValues();
var lss = SpreadsheetApp.getActiveSpreadsheet(); // Local spreadsheet - the one this script is in
var targetnumRows = lss.getSheetByName('Country A').getMaxRows();
var targetnumcolumns = lss.getSheetByName('Country A').getMaxColumns();
var ls = lss.getSheetByName('Country A').getRange(2,1,numRows,numColumn); // Local sheet - change to actual name and Local range - change to actual range
var TargetRange = lss.getSheetByName('Country A').getRange(2,1,targetnumRows,targetnumcolumns); // Local sheet - change to actual name and Local range - change to actual range
TargetRange.clear() //Clear Sheet Content
ls.setValues(values); // Copy from source sheet to local sheet
var sheet = lss.getSheetByName('Country A');
var maxColumns = sheet.getMaxColumns();
var lastColumn = sheet.getLastColumn();
var maxRows = sheet.getMaxRows();
var lastRow = sheet.getLastRow();
if (maxColumns-lastColumn != 0){sheet.deleteColumns(lastColumn+1, maxColumns-lastColumn)};
if (maxRows-lastRow != 0){sheet.deleteRows(lastRow+1, maxRows-lastRow)};
var TargetHour = lss.getSheetByName('Country A').getRange(1,1).setValue(Utilities.formatDate(new Date(), "CST", "MM-dd-yyyy HH:mm:ss"));
}
Please help me. Thank you.

The script stores the values in a 2D array. You have to iterate through the elements and delete the elements where column E (Col number 4) (A=0, B=1,...E=4) is not equal to "Mrm1, Mrm2, Mrm3".
Then you paste the modified array in the second sheet.
Here is an example. You need to adapt it to your code.
function copyData() {
var ss = SpreadsheetApp.openById("YOUR-ID");
// Stores data as a 2D Array
var data = ss.getSheetByName("Sheet1").getRange("A1:M10").getValues();
// Remove rows where column E not equals "Mrm1, Mrm2, Mrm3"
for (var i = 0; i < data.length; i++) {
if (data[i][4] !== "Mrm1" && data[i][4] !== "Mrm2" && data[i][4] !== "Mrm3") {
data.splice(i,1);
i--;
}
}
// Paste data in sheet 2
var pasteRange = "A1:M" + (data.length);
ss.getSheetByName("Sheet2").getRange(pasteRange).setValues(data);
}

Related

Add date and text at the last row after a copy paste

I'm breaking my head here trying to figure it out a way to achieve this in a simples way.
I got working a code that copies a range from a sheet and paste in another at the last row.
function copyInfo() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var copySheet = ss.getSheetByName("Review");
var pasteSheet = ss.getSheetByName("Log");
// get source range
var source = copySheet.getRange(3,2,15,5);
// get destination range
var destination = pasteSheet.getRange(pasteSheet.getLastRow()+1,3,15,5);
// copy values to destination range
source.copyTo(destination, SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
}
The problem is, at the destination sheet, I still have columns A and B empty. By each row in the source (varies from time to time) copied, I need to place at column A todays date and column B the text "Review".
Like: Date (column A) | Revview (column B) | Pasted Data from Source (column C and other)
Do I need to write a completely new code to check for empty or can I implement both solutions into this code?
The code should fill the two cells before your data. It also caters for the setup where you have three areas in the same sheet.
Each copyInfoXd_button function will be assigned to your button image.
function copyInfo1d_button() {
copyInfo(2, "B1:B");
}
function copyInfo7d_button() {
copyInfo(9, "I1:I");
}
function copyInfo30d_button() {
copyInfo(16, "P1:P");
}
function copyInfo(startingCol, subjectCol)
{
var ss = SpreadsheetApp.getActiveSpreadsheet();
var copySheet = ss.getSheetByName("Review");
var pasteSheet = ss.getSheetByName("Log");
const sourceCols = 4; //actual number of columns to copy, not including the empty column H
const destCols = 6; //actual number of columns to write to, from DATE to OBS
//find the last row from a colum
var avals = copySheet.getRange(subjectCol).getValues();
var lastRow = avals.filter(String).length;
var rowsToCopy = lastRow - 2; //skip the first 2 rows as they are headers
// get source range
var source = copySheet.getRange(3,startingCol,rowsToCopy,sourceCols); // row, column, number of rows and number of columns
// get destination range
var destination = pasteSheet.getRange(pasteSheet.getLastRow()+1,1,rowsToCopy,destCols);
var data = [];
var sourceValues = source.getValues();
//construct new data for the destination range
for (var row in sourceValues) {
var destRow = [Utilities.formatDate(new Date(), ss.getSpreadsheetTimeZone(), 'MMMM dd, yyyy'), 'Review'];
destRow = destRow.concat(sourceValues[row]);
data.push(destRow);
}
destination.setValues(data);
}
I made it! I'm so proud! Thank you all for the help!
I'm beginning to do stuff like this!
After the Charles answer I looked for a way to add to each row. However, I know the code might not be too elegant, and I couldn't figured it out a way of doing using script itself, so I added a helper cell inside the sheet in H1 and H2.
Here's the solution:
function copyInfo24() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var copySheet = ss.getSheetByName("Review");
var pasteSheet = ss.getSheetByName("Log");
// get source range
var source = copySheet.getRange(4,2,99,5);
// get destination range
var destination = pasteSheet.getRange(pasteSheet.getLastRow()+1,3,99,5);
// copy values to destination range
source.copyTo(destination, SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
// Sets date at Column A and "Review" at Column B.
var data = [];
for(var i =0; i < copySheet.getRange("H1").getValue(); i++) {
data.push([Utilities.formatDate(new Date(), ss.getSpreadsheetTimeZone(), 'MMMM dd, yyyy'), 'Review']);
}
reviewRange = pasteSheet.getRange(pasteSheet.getLastRow()-copySheet.getRange("H2").getValue(), 1, copySheet.getRange("H1").getValue(), 2)
reviewRange.setValues(data); //set the date and 'Review' data
}

Google App Script - how to Copy Paste values ( only non empty cells) of a data range with , from a Spreadsheet to another Spreadsheet

Objective - To copy all non-empty cells from a Spreadsheet-1 to Spreadsheet-2.
I saw some codes online and modified for my use, sharing below.
I need help in getting the script right so that a variable range(i.e. to selects non-empty up to last rows and last columns) can be selected and data copied to Spreadsheet-2.
I am trying to make it workable for 'A1:GZ1000' range.
If numbers of rows / cols with non-zero (non-empty) increases, script needs to update range for passing values to Spreadsheet-2.
''''
function dataImport(ssaID, ssbID) {
// source doc
var ssaID = '1DZX1Jb4qwCHETtAENIsRioJvl4TC0hUWZDn_k1q3oes' ;
var sheet = SpreadsheetApp.openById(ssaID);
var ssbID = '1HptKwYzdWCsz9oey9hOouhWNTQ3GAyu9oswZ1xgP3D0' ;
// source sheet
var ss = sheet.getSheetByName('Data - Price');
// Get full range of data
var SRange = ss.getDataRange();
// get A1 notation identifying the range
var A1Range = SRange.getA1Notation();
// get the data values in range
var SData = SRange.getValues();
// Logger.log(SData)
// target spreadsheet
var tss = SpreadsheetApp.openById(ssbID);
// target sheet
var ts = tss.getSheetByName('Data - Price');
// Clear the Google Sheet before copy
ts.clear({ contentsOnly: true });
Logger.log(SRange.length);
// set the target range to the values of the source data
var tsRange = ts.getRange(1,1,SRange.length,SRange[0].length);
tsRange.setValues(Sdata);
}
''''
Pl. do ask for details if you require.
R U
Try it this way:
function dataImport(ssaID, ssbID) {
var ssaID = '1DZX1Jb4qwCHETtAENIsRioJvl4TC0hUWZDn_k1q3oes';
var ss = SpreadsheetApp.openById(ssaID);
var ssbID = '1HptKwYzdWCsz9oey9hOouhWNTQ3GAyu9oswZ1xgP3D0';
var sh = ss.getSheetByName('Data - Price');
var rg = sh.getDataRange();
var vs = rg.getValues();
var tss = SpreadsheetApp.openById(ssbID);
var tsh = tss.getSheetByName('Data - Price');
tsh.clearContents();
tsh.getRange(1, 1, vs.length, vs[0].length).setValues(vs);
}

Google Script: copy values from one sheet to another determining the range

I have two sheets with unique IDs in one column. I need to make sure whenever a new ID is added in one sheet (Sheet1), it is copied to the last empty row in the other sheet (Sheet2). The IMPORTRANGE won't work as being dynamic any static information added in the other sheet would be irrelevant to the respective ID.
In another thread [1], I got help developing this script that will do exactly that. However, this script will insert the data in the other sheet in Column A. How can I modify the script so I can decide the specific range where I want the script to insert the data (in Sheet 2).
Update: I created this spreadsheet as an example. I'm trying to ensure that the script does the calculation (ie. adding vlaues that are not duplicated in the first available empty row), starting in cel C10 in "Sheet2". It would be also great if I can somehow change that range if I need to: https://docs.google.com/spreadsheets/d/1abESAXFrOHoqRQRNqQGfmAFxlbD10wlprAf1tca4y7o/edit#gid=132361488
Thanks!
function updateSheet() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = "Sheet1";
var destinationSheet = "Sheet2";
var source_sheet = ss.getSheetByName(sourceSheet);
var target_sheet = ss.getSheetByName(destinationSheet);
var lastCol = target_sheet.getLastColumn();
var lastRow = target_sheet.getLastRow();
if (lastRow > 1) { // <--- Added
var r = target_sheet.getRange(2,1, lastRow - 1, lastCol);
r.sort([{column: 1, ascending: true}]);
}
_updateSpreadsheet(source_sheet, target_sheet);
}
function _updateSpreadsheet(source_sheet, target_sheet) {
var last_row = target_sheet.getLastRow();
var source_data = source_sheet.getRange("A4:A" + source_sheet.getLastRow()).getValues(); // <--- Modified
var target_data = target_sheet.getDataRange().getValues();
var resultArray = [];
for (var n = 0 ; n < source_data.length ; n++) { // <--- Modified
var keep = true;
for(var p in target_data) {
if (source_data[n][0] == target_data[p][0]) {
keep = false; break;
}
}
var columnsToKeep = [0];
var tempData = [];
if(keep){
for(var c in columnsToKeep){ tempData.push(source_data[n][columnsToKeep[c]])}
resultArray.push(tempData);
}
}
last_row++;
resultArray = resultArray.filter(String); // <--- Added
if(resultArray.length>0){
target_sheet.getRange(last_row,1,resultArray.length,resultArray[0].length).setValues(resultArray);
}
}
[1] Google Script: Append new values in column to another sheet
target_sheet.getRange(last_row,1,resultArray.length,resultArray[0].length)
.setValues(resultArray);
This line
Takes the sheet stored in the variable target_sheet
Accesses the defined range
Sets values
The syntax to access a range used in your code is getRange(row, column, numRows, numColumns)
whereby the start column is numerical - in your case it's 1 which corresponds to column A.
If you want to modify the start column from A to B:
Just change your range definition from
getRange(last_row,1,resultArray.length,resultArray[0].length)
to
getRange(last_row,2,resultArray.length,resultArray[0].length)

How to get and set column number based on value in source spreadsheet

Trying to copy a range (L2:L9) to another spreadsheet based on a data validation selection (ID #) in a specific cell (G11) on the source spreadsheet.
The ID#s are all in alphanumeric order on the target spreadsheet along row 5, so all I need is to get the column number that corresponds to the choice made in the data validation field.
Before I had to convert the ID#s to Alphanumeric values, I simply had numeric values and the script worked fine. Now I'm having trouble converting the script to work with this modification.
function Submit() {
var ss = SpreadsheetApp.getActiveSpreadsheet(); // ss = source spreadsheet
var source_sheet = ss.getActiveSheet();
if (source_sheet.getName() == "Pre-Season CC") {
var SRange = source_sheet.getRange('L2:L9');
var A1Range = SRange.getA1Notation();
var SData = SRange.getValues();
var target = SpreadsheetApp.openById('1MpKdxFyBrVQT3EumePQvtCZpgzcDW5xlYe4B1DKZCA8');
var target_sheet = target.getSheetByName('Pre-Season');
var idNumber = source_sheet.getRange('Pre-Season CC!G11').getValue();
for (var i = 0; i < idNumber.length; i++) {
for (var j = 0; j < idNumber.length;j++){
if (target_sheet[4][j] == idNumber){
Logger.log((j+1))
return j+1;
target_sheet.getRange('B6:B13').offset(0,valueA1 = ss.getRange('Pre-Season!G11').getValue(idNumber)+1).setValues(SData);
}
}
}
}
At that point, I need the data copied from source spreadsheet onto the target spreadsheet in it's designated column, according to the ID#. Please help!
Try below code.
function Submit() {
var ss = SpreadsheetApp.getActiveSpreadsheet(); // ss = source spreadsheet
var source_sheet = ss.getActiveSheet();
if (source_sheet.getName() == 'Pre-Season CC') {
// source sheet items
var SData = source_sheet.getRange('L2:L9').getValues();
var idNumber = source_sheet.getRange('Pre-Season CC!G11').getValue();
// target sheet items
var targetSS = SpreadsheetApp.openById('1MpKdxFyBrVQT3EumePQvtCZpgzcDW5xlYe4B1DKZCA8');
var target_sheet = targetSS.getSheetByName('Pre-Season');
// 5 = row where IDs are
var TData = target_sheet.getRange(5, 1, 1, target_sheet.getLastColumn()).getValues()[0];
// column index of #ID in target sheet
var colIndex = TData.indexOf(idNumber) + 1;
// write data in that column, rows from 6 to 13
target_sheet.getRange(6, colIndex, SData.length, 1).setValues(SData);
}
}

COPYING DATA FROM ONE SPREADSHEET TO ANOTHER

I have three source files - Alpha, Beta, Kappa. They have same headers and basically, I just want them all consolidated in one file on a weekly basis. I started with Kappa and somehow it works - it creates a new spreadsheet and copies info from the Kappa file and paste it to the newly created spreadsheet as values.
Now I tried, adding the Alpha file to the same spreadsheet, just below the info that I copied from the Kappa file using the getLastRow function. I got an error saying that source range and target range must be on the same spreadsheet.
function RawExtractCopy() {
var version = 'ver. 4.01'
var ssnew = SpreadsheetApp.create('V5 Raw Extract '+ version ); //create a spreadsheet
var ssnewsheet = ssnew.getSheetByName('Sheet1'); //get the sheet named Sheet1
ssnewsheet.insertRows(1,30000); //inserting more rows
ssnewsheet.setName('V5 Raw Extract'); //rename the sheet name of the newly created spreadsheet
var ssKappa = SpreadsheetApp.getActiveSpreadsheet(); //opens source file
var targetss = ssnew; //define created ss as the target ss
var srcSheetKappa = ssKappa.getSheetByName('KAPPA'); //get source sheet name
var targetSheet = targetss.getSheetByName('V5 Raw Extract'); //defining target sheet
var srcRangeKappa = srcSheetKappa.getRange("A1:AM30000"); //get source data
var destRangeKappa = targetss.getRange("A1"); //define target
var values = srcRangeKappa.getValues(); //line 18 to 21 is just to match source sheet and target sheet
var bGcolors = srcRangeKappa.getBackgrounds();
var colors = srcRangeKappa.getFontColors();
var fontSizes = srcRangeKappa.getFontSizes();
destRangeKappa.setValues(values);
destRangeKappa.setBackgrounds(bGcolors);
destRangeKappa.setFontColors(colors);
destRangeKappa.setFontSizes(fontSizes);
srcRangeKappa.copyTo(destRangeKappa, {contentsOnly: true});
var ssAlpha = SpreadsheetApp.openById("1gIs4vCdcGG79poDujz9t8Fq7_BWlaEiMrYUH9DTxVHs").activate();
var srcSheetAlpha = ssAlpha.getSheetByName('V5 ALPHA');
var srcRangeAlpha = srcSheetAlpha.getRange("A2:AM30000");
var destRangeAlpha = targetSheet.getRange(targetSheet.getLastRow()+1,1);
var valuesAlpha = srcRangeAlpha.getValues();
var bGcolorsAlpha = srcRangeAlpha.getBackgrounds();
var colorsAlpha = srcRangeAlpha.getFontColors();
var fontSizesAlpha = srcRangeAlpha.getFontSizes();
destRangeAlpha.setValues(values);
destRangeAlpha.setBackgrounds(bGcolors);
destRangeAlpha.setFontColors(colors);
destRangeAlpha.setFontSizes(fontSizes);
srcRangeKappa.copyTo(destRangeAlpha, {contentsOnly: true});
}
The thing, it still copies the data from Kappa but did not push through with Beta. Can someone please be kind to tell what I am missing here?
The OP's goal is to make a duplicate of three sheets in three separate spreadsheets; Alpha, Beta and Kappa. The OP's code works for Kappa, but not Alpha or Beta.
Why does this code work and the OP code doesn't
Well, the OP code does work, but the OP fudges by specifying a fixed range (30000 rows long and about 40 column wide). This works for the first sheet, but not so easily for the second and third sheets. The following code differs in two major respects from the OP code:
the data ranges in Alpha, Beta, Kappa and "Raw Extract" are evaluated using getLastRow and getLastColumn. So the source range and the target range will match each other. In addition, data from the second and third sheets is easily appended one row below the preceding last row.
the copyTo code is changed to copy the whole sheet rather than a range. The reason for this is the the preceding four commands setValues(values), setBackgrounds(bGcolors), setFontColors(colors), and setFontSizes(fontSizes) have already copied the data range to "Raw Extract".
function so_55448299() {
var version = 'ver. 4.01'
var ssnew = SpreadsheetApp.create('V5 Raw Extract ' + version); //create a spreadsheet
var ssnewsheet = ssnew.getSheetByName('Sheet1'); //get the sheet named Sheet1
ssnewsheet.setName('V5 Raw Extract'); //rename the sheet name of the newly created spreadsheet
// copy Kappa and data
var ssKappa = SpreadsheetApp.getActiveSpreadsheet(); //opens source file
var targetss = ssnew; //define created ss as the target ss
var srcSheetKappa = ssKappa.getSheetByName('KAPPA'); //get source sheet name
var targetSheet = targetss.getSheetByName('V5 Raw Extract'); //defining target sheet
// get the last row and column of the source sheet
var kappaLastRow = srcSheetKappa.getLastRow();
var kappaLastCol = srcSheetKappa.getLastColumn();
//Logger.log("DEBUG: Kappa Last row = "+kappaLastRow+", Last column = "+kappaLastCol);//DEBUG
// declare the source and target ranges
var srcRangeKappa = srcSheetKappa.getRange(1, 1, kappaLastRow, kappaLastCol); //get source data
var destRangeKappa = targetSheet.getRange(1, 1, kappaLastRow, kappaLastCol); //define target
// get values and other data
var values = srcRangeKappa.getValues(); //line 18 to 21 is just to match source sheet and target sheet
var bGcolors = srcRangeKappa.getBackgrounds();
var colors = srcRangeKappa.getFontColors();
var fontSizes = srcRangeKappa.getFontSizes();
// set values and other data
destRangeKappa.setValues(values);
destRangeKappa.setBackgrounds(bGcolors);
destRangeKappa.setFontColors(colors);
destRangeKappa.setFontSizes(fontSizes);
// duplicate the entire sheet
srcSheetKappa.copyTo(targetss);
// end copy Kappa and data
// start copy Alpha and data
var ssAlpha = SpreadsheetApp.openById("<insert code here>");
var srcSheetAlpha = ssAlpha.getSheetByName('V5 ALPHA');
// get the last row and column of the source sheet
var alphaLastRow = srcSheetAlpha.getLastRow();
var alphaLastCol = srcSheetAlpha.getLastColumn();
//Logger.log("DEBUG: Alpha Last row = "+alphaLastRow+", Alpha Last Column = "+alphaLastCol);//DEBUG
// get the last row and column of the target sheet
var rawLastRow = targetSheet.getLastRow();
var rawLastCol = targetSheet.getLastColumn();
Logger.log("DEBUG: Target Last row = " + rawLastRow + ", Target Last Column = " + rawLastCol); //DEBUG
// declare the source and target ranges
var srcRangeAlpha = srcSheetAlpha.getRange(2, 1, alphaLastRow - 1, alphaLastCol);
var destRangeAlpha = targetSheet.getRange(rawLastRow + 1, 1, alphaLastRow - 1, alphaLastCol);
//Logger.log("DEBUG:destRangeAlpha = "+destRangeAlpha.getA1Notation());//DEBUG
// get values and other data
var values = srcRangeAlpha.getValues();
var bGcolors = srcRangeAlpha.getBackgrounds();
var colors = srcRangeAlpha.getFontColors();
var fontSizes = srcRangeAlpha.getFontSizes();
// set values and other data
destRangeAlpha.setValues(values);
destRangeAlpha.setBackgrounds(bGcolors);
destRangeAlpha.setFontColors(colors);
destRangeAlpha.setFontSizes(fontSizes);
// duplicate the entire sheet
srcSheetAlpha.copyTo(targetss);
// end copy Alpha and data
// start copy Beta and data
var ssBeta = SpreadsheetApp.openById("<insert code here>");
var srcSheetBeta = ssBeta.getSheetByName('V5 BETA');
// get the last row and column of the source sheet
var betaLastRow = srcSheetBeta.getLastRow();
var betaLastCol = srcSheetBeta.getLastColumn();
//Logger.log("DEBUG: Beta Last row = "+betaLastRow+", Beta Last Column = "+betaLastCol);//DEBUG
// get the last row and column of the target sheet
var rawLastRow = targetSheet.getLastRow();
var rawLastCol = targetSheet.getLastColumn();
//Logger.log("DEBUG: Target Last row = "+rawLastRow+", Target Last Column = "+rawLastCol);//DEBUG
// declare the source and target ranges
var srcRangeBeta = srcSheetBeta.getRange(2, 1, betaLastRow - 1, betaLastCol);
var destRangeBeta = targetSheet.getRange(rawLastRow + 1, 1, betaLastRow - 1, betaLastCol);
//Logger.log("DEBUG:destRangeBeta = "+destRangeBeta.getA1Notation());//DEBUG
// get values and other data
var values = srcRangeBeta.getValues();
var bGcolors = srcRangeBeta.getBackgrounds();
var colors = srcRangeBeta.getFontColors();
var fontSizes = srcRangeBeta.getFontSizes();
// set values and other data
destRangeBeta.setValues(values);
destRangeBeta.setBackgrounds(bGcolors);
destRangeBeta.setFontColors(colors);
destRangeBeta.setFontSizes(fontSizes);
// duplicate the entire sheet
srcSheetBeta.copyTo(targetss);
// end copy Beta and data
}
UPDATE - One Small Function
#tehhowch rightly observes that a more appropriate way to manage this process would be one small function called with a few parameters from a driver function. This is something that I had in mind at the time but (for better or worse) I felt that the long-hand approach would enable the OP to better understand how the code varied from their own. However, this variation seeks to fulfil tehhowch's observation. I have left in the Logger statements for the benefit of the OP (which explains the extraordinary length of the code
function so_55448299_04() {
//Note#1: this function assumes that it is located in the "Kappa" sheet
//Note#2: the spreadsheet ID for Alpha and Beta cannot be assigned to a variable. they must be entered longhand BEFORE this function is processed.
//Note#3: the target sheet names are also entered longhand. BUT the number of sheets is described in the variable "usersheets"
// user defined variables
var targetVn = 'ver. 4.01';
var targetName = 'V5 Raw Extract';
var usersheets = 3;
// create the targetspreadsheet and starting sheet
var targetss = SpreadsheetApp.create(targetName + " " + targetVn);
var targetSheet = targetss.getSheetByName('Sheet1');
targetSheet.setName(targetName);
//loop through the usersheets
for (var i = 0; i < usersheets; i++) {
// if i==0, then process this sheet - KAPPA
if (i == 0) {
//Logger.log("DEBUG: This is KAPPA");//DEBUG
var srcss = SpreadsheetApp.getActiveSpreadsheet();
//Logger.log("DEBUG: this spreadsheet is "+srcss.getName());//DEBUG
// startrow is 1 in order to include headers
var startrow = 1;
var srcSheet = srcss.getSheetByName("v5 KAPPA");
}
// if i=1, then process Alpha
else if (i == 1) {
//Logger.log("DEBUG: This is ALPHA");//DEBUG
var srcss = SpreadsheetApp.openById("<Insert code>");
//Logger.log("DEBUG: this spreadsheet is "+srcss.getName());//DEBUG
// startrow is 2 in iorder to avoid duplicating headers
var startrow = 2;
var srcSheet = srcss.getSheetByName("v5 ALPHA");
}
// if i=2, then process Beta
else if (i == 2) {
//Logger.log("DEBUG: This is BETA");//DEBUG
var srcss = SpreadsheetApp.openById("<Insert code>");
//Logger.log("DEBUG: this spreadsheet is "+srcss.getName());//DEBUG
// startrow is 2 in iorder to avoid duplicating headers
var startrow = 2;
var srcSheet = srcss.getSheetByName(usersheets[i]);
}
// run the subroutine to copy and paste data
// Logger.log("DEBUG: srcSheet: "+srcSheet.getName()+", targetSheet: "+targetSheet.getName()+", startrow: "+startrow+", targetss: "+targetss.getName());//DEBUG
var getresult = getData04(srcSheet, targetSheet, startrow, targetss);
}
}
function getData04(srcSheet, targetSheet, startrow, targetss) {
//get the source sheet - last row and column
var srcLastRow = srcSheet.getLastRow();
var srcLastCol = srcSheet.getLastColumn();
//Logger.log("DEBUG: Source Last row = "+srcLastRow+", Last column = "+srcLastCol);//DEBUG
// get the target sheet - last row and column
var targetLastRow = targetSheet.getLastRow();
var targetLastCol = targetSheet.getLastColumn();
//Logger.log("DEBUG: Target Last row = "+targetLastRow+", Target Last Column = "+targetLastCol);//DEBUG
// declare the source and target ranges
if (startrow == 1) {
var srcRange = srcSheet.getRange(startrow, 1, srcLastRow, srcLastCol);
var targetRange = targetSheet.getRange(startrow, 1, srcLastRow, srcLastCol);
} else {
var srcRange = srcSheet.getRange(startrow, 1, srcLastRow - 1, srcLastCol);
var targetRange = targetSheet.getRange(targetLastRow + 1, 1, srcLastRow - 1, srcLastCol);
}
//Logger.log("DEBUG: srcRange = "+srcRange.getA1Notation()+", target range = "+targetRange.getA1Notation());//DEBUG
// get source values and other data
var values = srcRange.getValues();
var bGcolors = srcRange.getBackgrounds();
var colors = srcRange.getFontColors();
var fontSizes = srcRange.getFontSizes();
// set values and other data
targetRange.setValues(values);
targetRange.setBackgrounds(bGcolors);
targetRange.setFontColors(colors);
targetRange.setFontSizes(fontSizes);
// duplicate the entire sheet
srcSheet.copyTo(targetss);
var result = "Successful";
return result;
}