I am trying to use Apps Script to query 2 datasets and compare certain columns across them. I am hoping to...
a) identify missing ID values;
b) reconcile differences in other fields, when the ID values match.
INPUT:
Spreadsheet with 2 tabs (tab1, tab2).
The key ID in each B column (Btab1, Btab2)
I want to identify instances where a unique value (B) is in one dataset but not in the other (the rows are not in the same order)
Run a function & push to an output tab if Btab1 is not in tab2 || Btab2 is not in tab1
When a value of B is in both tabs (the majority of the time), I want to identify instances of data discrepancies in a few columns...
For all instances of B, push B and the relevant columns below to the output tab if...
Column M in tab1 doesn't match column E in tab2
Column P in tab1 <> column F in tab2
Column AN tab1 <> Column G tab2
OUTPUT:
tab that displays problem areas in the datasets.
First column is ID Key.
Second column explains the issue via text string
Again, the challenge here is that the values are not sorted the same, and there could be a slight difference in total # rows
function compare() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
ss.insertSheet(1);
ss.getActiveSheet().setName('output');
var sheet1 = ss.getSheetByName('sheet1');
var sheet2 = ss.getSheetByName('sheet2');
var sheet_output = ss.getSheetByName('output');
var range1 = sheet1.getRange(1,1,sheet1.getLastRow(),sheet1.getLastColumn()).getValues();
var output1 = [];
var a1;
var b1;
var h1;
var i1;
var j1;
var m1;
var o1;
var p1;
var an1;
var ao1;
var x;
var range2 = sheet2.getRange(1,1,sheet2.getLastRow(),sheet2.getLastColumn()).getValues();
var output2 = [];
var a2;
var b2;
var c2;
var d2;
var e2;
var f2;
var g2;
var h2;
var y;
/// can i do for(x in range1; y in range2) { all in one function?? If so, what is the proper syntax?
for(x in range1, y in range2) {
a1 = range1[x][0];
b1 = range1[x][1];
h1 = range1[x][7];
i1 = range1[x][8];
j1 = range1[x][9];
m1 = range1[x][12];
o1 = range1[x][14];
p1 = range1[x][15];
an1 = range1[x][39];
ao1 = range1[x][40];
a2 = range2[y][0];
b2 = range2[y][1];
c2 = range2[y][2];
d2 = range2[y][3];
e2 = range2[y][4];
f2 = range2[y][5];
g2 = range2[y][6];
h2 = range2[y][7];
if (
(b1 != b2) ||
(m1 != e2) // etc etc etc
)
{
//push to output
}}
Whilst your syntax for(x in range1, y in range2) will not return an error, it won't give you the desired result neither if the rows are not in the same order
Reason:
During each iteration both x and y will change, e.g. if var range1 = [1,2,3] and var range2 = [4,5,6], your loop will iterate 3 times and the values in your sample loop iterations will be:
iteration
range1[x] = 1 and range2[y] = 4
iteration
range1[x] = 2 and range2[y] = 5
iteration
range1[x] = 3 and range2[y] = 6
In this case you will not retrieve the combination
range1[x] = 1 and range2[y] = 4
or
range1[x] = 2 and range2[y] = 6
and so on.
Instead you need to use two nested for loops, which would iterate through all possible combinations of x and y:
for(x in range1) {
for(y in range2){
...
}
}
Sidenote:
Even if your rows would be in the same order, you still need to be careful. Because for(x in range1) opposed to for(x = 0; x < range1.length; i++) gives you no control about in which folder the loop will iterate over the range.
Now to your query for duplicates
A possible way to implement the functionality in a not too complicated manner would be the following:
Define boolean variable and use it to check for each x either it has a duplicate
If a duplicate (for column B) is found - further criteria will be evaluated
If two rows match by all criteria, the inner loop will be exited with break and the function will jump to the next x
If rows with identical key IDs, but discrepancies in other columns are found - both rows will be pushed into sheet output for comparison purposes (this is easier to implement than specifying what exactly is discrepant)
After this the inner loop will also be exited
In oth cases above duplicate will be set to true
If a unique Id is found in sheet1 (duplicate = false) - it will be immediately pushed into output
Sample
function compare() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
ss.insertSheet(1);
ss.getActiveSheet().setName('output');
var sheet1 = ss.getSheetByName('sheet1');
var sheet2 = ss.getSheetByName('sheet2');
var sheet_output = ss.getSheetByName('output');
var range1 = sheet1.getRange(1,1,sheet1.getLastRow(),sheet1.getLastColumn()).getValues();
var output1 = [];
var b1;
var m1;
var p1;
var an1;
var x;
var range2 = sheet2.getRange(1,1,sheet2.getLastRow(),sheet2.getLastColumn()).getValues();
var output2 = [];
var b2;
var e2;
var f2;
var g2;
var y;
var array = [];
for(x in range1) {
var duplicate = false;
for(y in range2){
b1 = range1[x][1];
m1 = range1[x][12];
p1 = range1[x][15];
an1 = range1[x][39];
b2 = range2[y][1];
e2 = range2[y][4];
f2 = range2[y][5];
g2 = range2[y][6];
if (
(b1 == b2)
)
{
Logger.log("found");
duplicate = true;
if((m1 != e2)||
(p1 != f2) ||
(an1 != g2)){
array.push(range1[x]);
array.push(range2[y]);
}
break;
}
}
if (duplicate == false){
Logger.log("duplicate false");
array.push(range1[x]);
}
}
//push to output
if(array[0]){
sheet_output.getRange(sheet_output.getLastRow()+1, 1, array.length, array[0].length).setValues(array);
}
}
Related
I'm trying to make a google sheet script that adds a row based on cell value, basically if I have in the Quantity (Column D) 7x laptops, I want the script to add 6 additional rows below if Column H is marked as "Yes" through data validation.
What I was able to find and to do is only duplicate that row but is without data validation and I would prefer to add the data validation and possible make each quantity split to 1 (instead of 7) after the duplication.
`function autoDup() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var data = sheet.getDataRange().getValues();
var newData = [];
for(var n in data){
newData.push(data[n]);
if(!Number(data[n][3])){continue};// if column 3 is not a number then do nothing
for(var c=1 ; c < Number(data[n][3]) ; c++){ // start from 1 instead of 0 because we have already 1 copy
newData.push(data[n]);//store values
}
}
sheet.getRange(1,1,newData.length,newData[0].length).setValues(newData).sort({column: 1, ascending: false});// write new data to sheet, overwriting old data
}`
Hope someone is able to help me.
Thank you,
Column D contains a qty and goods description. If Column H = "Yes", you want to insert a number of rows below Col D equal to the qty minus one. If Column H <> "Yes, then take no action.
Sample data - Before
Sample data - After
function so5925663201() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheetname = "59256632";
var sheet = ss.getSheetByName(sheetname);
var row = 7;
// get value of Column H
var colHValue = sheet.getRange(row,8).getValue();
if (colHValue === "Yes"){
//Logger.log("DEBUG: Col H = yes. do something")
// get value of Column D
var Value = sheet.getRange(row,4).getValue();
var searchterm = "x";
var indexOfFirst = Value.indexOf(searchterm);
//Logger.log("DEBUG: the first instance of 'x' is "+indexOfFirst);
// get the quantity and convert from a string to a number
var qty = Value.substring(0, indexOfFirst);
var qtynum = +qty;
// var newtype = typeof qtynum; // DEBUG
//Logger.log("DEBUG: the quantity is "+qtynum+", new type = "+newtype)
// This inserts rows after
sheet.insertRowsAfter(row, qtynum-1);
}
else{
//Logger.log("DEBUG: col H <> Yes. do nothing");
}
}
I am currently trying to exchange values between two columns (G and H)
The first sheet column G values don't move if they are found in a first column table reference in another sheet.
if they are found in second column table reference,
they have to swap values with the first sheet column H values the same index.
This code works for column G but not for column H. It gives the elements but not in order.
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet29'); //au cas ou ca bug
var lr = spreadsheet.getLastRow();
var lc = spreadsheet.getLastColumn();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet1 = ss.getSheetByName('Sheet29');
var sheet2 = ss.getSheetByName('Tabs of applications and firmwares');
var range1 = sheet1.getRange(2, 7, sheet1.getLastRow(), 9);
var range2 = sheet2.getRange(2, 1, sheet2.getLastRow(), 4);
var values1 = range1.getValues();
var values2 = range2.getValues();
var compteurBon = 0;
var compteurMauvais = 0;
var tempArray1 = [];
for (var i = 0; i < values1.length; i++) {
for (var j = 0; j < values2.length; j++) {
if (values1[i][0] === values2[j][0]) {
compteurBon++;
} else if (values1[i][0] == values2[j][2]) {
compteurMauvais++;
var tempArrayValue1 = values1[i][0];
tempArray1.push(tempArrayValue1);
values1[i][0] = values1[i][1];
values1[i][1] = tempArray1; // here it doesn't take the right value
}
}
}
range1.setValues(values1);
};
Okay, I think your code is in the good direction but it can be simplified. First of all, I understand that we have two sheets (“Sheet 29” and “Tabs of applications and firmwares”), and you want to copy the values from H to G in case G equals B from the second sheet. As I said in my comment, in case you want to just get two columns in the same range, you should use sheetx.getRange(2, col, sheet1.getLastRow(), 2); This last '2' is the number of columns you are getting into the range (G and H, A and B respectively). If we stick to your approach, we will need another tempArray variable:
for (var i = 0; i < values1.length - 1; i++) {
tempArray1.push(values1[i].toString().split(","));
tempArray2.push(values2[i].toString().split(","));
}
What we are doing here is putting the 4 columns in those two arrays. Each one of them is 2D, so tempArray1 contains all the rows from G and H and the same for tempArray2 with A and B. You can access the data with [x][0] and [x][1] respectively.
After this you can just compare the values of temparray1 and temparray2, and write the values from G (temparray[i][1]) to H (temparray[i, 7])
if (tempArray1[i][0] == tempArray2[i][0]) {
compteurBon++;
} else if (tempArray1[i][0] == tempArray2[i][1]) {
sheet1.getRange(i+2 , 7).setValue(tempArray1[i][1]);
}
I need to update the value of "product quantity" based on the value of "order quantity" but only when "order sku" is equal to "product sku".
function productLoop2() {
var app = SpreadsheetApp;
var ss = app.getActiveSpreadsheet();
var activeSheet = ss.getActiveSheet();
var t = 2;
var n = 2;
var s = 0;
for(var t = 2; t < 52; t++) {
var x = activeSheet.getRange(t, 1).getValue();
//x is the ORDER SKU
var r = activeSheet.getRange(t, 2).getValue();
//r is the ORDER QUANTITY
var q = activeSheet.getRange(n, 3).getValue();
//q is the PRODUCT SKU
var u = activeSheet.getRange(n, 4).getValue();
//u is the PRODUCT QUANTITY
if (x != q) {
n++;
} else {
s = u - r;
}
var m = activeSheet.getRange(n,4).setValue(s);
}
}
I need the cell "n,4" (order quantity) to update so the value equals the result of "u"(product quantity) minus "r"(order quantity)
The code "if" fragment should be corrected as below:
if (x != q) {
n++;
} else {
activeSheet.getRange(n,4).setValue(u - r);
}
Update after discussion:
function updateProductQuantities() {
var activeSheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var values = activeSheet.getDataRange().getValues();
// Sum quantities by orders (columns A and B)
var sku = {};
for (var i in values) {
if (i == 0) continue; // headers row
if (sku[values[i][0]] == undefined) {
sku[values[i][0]] = values[i][1];
} else {
sku[values[i][0]] += values[i][1];
}
}
// Update product quantities (columns C and D)
for (i in values) {
if (sku[values[i][2]] != undefined) {
values[i][3] -= sku[values[i][2]];
}
}
// return values to the sheet
activeSheet.getDataRange().setValues(values);
}
You should use 2 "for" loops. One is for sum of orders quantities, and the other is for subtraction.
Here is how you can get all the data, modify it and set it in the sheet.
function productLoop2() {
var sheet = SpreadsheetApp.getActive().getActiveSheet();
// Get the active sheet of the active spreadsheet
var orderRange = sheet.getRange('A2:B52');
var productRange = sheet.getRange('C2:D52');
// Get the data ranges (change these references as necessary)
var orderData = orderRange.getValues();
var productData = productRange.getValues();
// Get the values from the ranges. This method returns an array.
for (var row = 0; row < orderData.length; row++) {
// Loops through every row of the order array
// Arrays are zero-based; this means the first element is element 0,
// the second element in element 1 and so on.
// Data is accessed with [row index][column index];
var oSku = orderData[row][0];
var oQty = orderData[row][1];
for (var productIndex = 0; productIndex < productData.length; productIndex++) {
// Loops through every product in the product array
var pSku = productData[productIndex][0];
var pQty = productData[productIndex][1];
if (oSku === pSku) {
productData[productIndex][1] = pQty - oQty;
// Changes the pQty value in the array
break;
// Added upon suggestion from user tehhowch
}
}
}
productRange.setValues(productData);
// Sets all product values in the array to the range from which they were taken
}
References:
Multidimensional arrays
Best practices - batch operations
For loops
The code below is from an answer from this post regarding copying row values to a new sheet if it exist in another sheets.
Now, what if instead of copying the duplicate values to sheet 3, I want to delete them from sheets 1 and 2 if it exists in Sheet 3. With the same spreadsheet, I have 3 sheets. The unique value that will be compared on the first 2 sheets is the first column, "ID NUMBER".
Given the values, 784 | John Steep | I.T Department, which exists in all 3 sheets, the same row value should be deleted in Sheet 1 and 2 and retain the same value on Sheet 3.
function copyRowtoSheet3() {
var s1 = SpreadsheetApp.openById("ID").getSheetByName('Sheet1');
var s2 = SpreadsheetApp.openById("ID").getSheetByName('Sheet2');
var s3 = SpreadsheetApp.openById("ID").getSheetByName('Sheet3');
var values1 = s1.getDataRange().getValues();
var values2 = s2.getDataRange().getValues();
var resultArray = [];
for(var n=0; n < values1.length ; n++){
var keep = false;
for(var p=0; p < values2.length ; p++){
Logger.log(values1[n][0]+' =? '+values2[p][0]);
if( values1[n][0] == values2[p][0] && values1[n][3] == values2[p][4]){
resultArray.push(values1[n]);
Logger.log('true');
break ;// remove this if values are not unique and you want to keep all occurrences...
}
}
}
s3.getRange(+1,1,resultArray.length,resultArray[0].length).setValues(resultArray);
}
Can't seem to find the right solution. Tried several scripts but failed to make it work.
Thank you for any advice/suggestion.
Although the other answer works (I didn't test but I guess it does) it uses a lot of spreadsheetApp calls and might be slow if you have a lot of data.
It is possible to get this result using only arrays (if you don't need to keep sheet formatting and/or formulas).
The approach is slightly different as it is easier to keep data instead of removing it.
There are for sure many possible solutions, below is the one I tried : I created a special array that contains only the first column of sheet3 to make the duplicate search simpler.
function removeDupsInOtherSheets() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s1 = ss.getSheetByName("Sheet1").getDataRange().getValues();
var s2 = ss.getSheetByName("Sheet2").getDataRange().getValues();
var s3 = ss.getSheetByName("Sheet3").getDataRange().getValues();
// iterate s3 and check in s1 & s2 if duplicate values exist
var nS1 = [];
var nS2 = [];
var s3Col1 = [];// data in column1 of sheet3
for(var n in s3){
s3Col1.push(s3[n][0]);
}
for(var n in s1){ // iterate sheet1 and test col 1 vs col 1 in sheet3
var noDup1 = checkForDup(s1[n],s3Col1)
if(noDup1){nS1.push(noDup1)};// if not present in sheet3 then keep
}
for(var n in s2){ // iterate sheet2 and test col 1 vs col 1 in sheet3
var noDup2 = checkForDup(s2[n],s3Col1)
if(noDup2){nS2.push(noDup2)};// if not present in sheet3 then keep
}
Logger.log(nS1);// view result
Logger.log(nS2);
ss.getSheetByName("Sheet1").getDataRange().clear();// clear and update sheets
ss.getSheetByName("Sheet2").getDataRange().clear();
ss.getSheetByName("Sheet1").getRange(1,1,nS1.length,nS1[0].length).setValues(nS1);
ss.getSheetByName("Sheet2").getRange(1,1,nS2.length,nS2[0].length).setValues(nS2);
}
function checkForDup(item,s){
Logger.log(s+' = '+item[0]+' ?')
if(s.indexOf(item[0])>-1){
return null;
}
return item;
}
Sheet1
ID NUMBER NAME DEPARTMENT
784 John Steep I.T.
901 Liz Green H.R.
Sheet2
ID NUMBER NAME DEPARTMENT
784 John Steep I.T.
653 Bo Gore Marketing
Sheet3
ID NUMBER NAME DEPARTMENT
784 John Steep I.T.
999 Frank White Sales
121 Abid Jones Engineering
901 Liz Green H.R.
Script
function main() {
var ss = SpreadsheetApp.openById("ID");
var s1 = ss.getSheetByName("Sheet1");
var s2 = ss.getSheetByName("Sheet2");
var s3 = ss.getSheetByName("Sheet3");
var idCol = 1; // Assuming location of ID column is same in all sheets.
var s1RowCount = s1.getLastRow();
for (var i = 2; i <= s1RowCount; i++) { // Start at var i = 2 to skip the
// first row containing the header.
var id = s1.getRange(i, idCol, 1, 1).getValue();
deleteDuplicates(s2, id);
deleteDuplicates(s3, id);
}
}
function deleteDuplicates(sheet, id) {
var idCol = 1; // Assuming location of ID column is same in all sheets.
var rowCount = sheet.getLastRow();
for (var i = 2; i <= rowCount; i++) {
var data = sheet.getRange(i, idCol, 1, 1).getValue();
if (data === id) {
// Use this to test out the function.
Logger.log("Duplicate of ID " + id + " in sheet " +
sheet.getSheetName() + " at row " + i);
// Uncomment the next line when ready.
// sheet.deleteRow(i);
}
}
}
Logging Output
[14-11-04 09:16:04:551 PST] Duplicate of ID 784 in sheet Sheet2 at row 2
[14-11-04 09:16:04:587 PST] Duplicate of ID 784 in sheet Sheet3 at row 2
[14-11-04 09:16:04:727 PST] Duplicate of ID 901 in sheet Sheet3 at row 5
I am new to Google Script and I have a script to create. I found this question, where in he should delete row if a value in it exists in another sheet. Now, my situation is different. Here is my sample Spreadsheet. In that Google Spreadsheet, I have 3 sheets. The unique value that will be compared on the first 2 sheets is the first column, "ID NUMBER".
Given the values, 784 | John Steep | I.T Department exists in the first 2 sheets therefore the whole row should be copied to sheet3.
Sum up, if that ID NUMBER exists in Sheet 1 and 2, it should be copied on Sheet 3.
I tried to modify the script but I can't make it work:
function copyRowtoSheet3() {
var s1 = SpreadsheetApp.openById("1RlQTLZyPLasoJGplKemKg9qgcLcvCZZ_tPn6lWXEePw").getSheetByName('Sheet1');
var s2 = SpreadsheetApp.openById("1RlQTLZyPLasoJGplKemKg9qgcLcvCZZ_tPn6lWXEePw").getSheetByName('Sheet2');
var s3 = SpreadsheetApp.openById("1RlQTLZyPLasoJGplKemKg9qgcLcvCZZ_tPn6lWXEePw").getSheetByName('Sheet3');
var values1 = s1.getDataRange().getValues();
var values2 = s2.getDataRange().getValues();
var resultArray = [];
for(var n in values1){
var keep = true
for(var p in values2){
if( values1[n][0] == values2[p][0] && values1[n][1] == values2[p][1]){
keep=false ; break ;
}
}
if(keep){
resultArray.push(values1[n])};
}
s1.clear()
s1.getRange(1,1,resultArray.length,resultArray[0].length).setValues(resultArray);
}
Thanks. Any help/advice is greatly appreciated.
Not sure your condition works... Getting the values that are equal in both sheets is easier than the example you refer to, just keep the data where equality==true.
Try like this (change the ID's to yours):
function copyRowtoSheet3() {
var s1 = SpreadsheetApp.openById("1x8buwr______w7MeqZAiJJIX0yC-oITBAtykBAM").getSheetByName('Sheet1');
var s2 = SpreadsheetApp.openById("1x8buwr______w7MeqZAiJJIX0yC-oITBAtykBAM").getSheetByName('Sheet2');
var s3 = SpreadsheetApp.openById("1x8buwr______w7MeqZAiJJIX0yC-oITBAtykBAM").getSheetByName('Sheet3');
var values1 = s1.getDataRange().getValues();
var values2 = s2.getDataRange().getValues();
var resultArray = [];
for(var n=0; n < values1.length ; n++){
var keep = false;
for(var p=0; p < values2.length ; p++){
Logger.log(values1[n][0]+' =? '+values2[p][0]);
if( values1[n][0] == values2[p][0] && values1[n][1] == values2[p][1]){
resultArray.push(values1[n]);
Logger.log('true');
break ;// remove this if values are not unique and you want to keep all occurrences...
}
}
}
s3.getRange(+1,1,resultArray.length,resultArray[0].length).setValues(resultArray);
}
Try:
var s1 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet 1');
var s2 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet 2');
var s3 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet 3');
and further:
s3.clear()
s3.getRange(1,1,resultArray.length,resultArray[0].length).setValues(resultArray);