Check duplicates between all sheets in google sheets - google-apps-script

What I want to do is check for duplicates between all google sheets and if there are any - mark them in a color. For now, I left the headers to my columns just to see if it marks them red. I found one script that everyone said that worked - however, I got really random results with it. Could you possibly help with it?
The script:
Array.prototype.countItem = function (item) {
var counts = {};
for (var i = 0; i < this.length; i++) {
var num = this[i];
counts[num] = counts[num] ? counts[num] + 1 : 1;
}
return counts[item] || 0;
}
function findDuplicatesOnAllSheets() {
var nondupes = [];
SpreadsheetApp.getActive()
.getSheets()
.forEach(function (s) {
s.getRange('A1:E500')
.getValues()
.reduce(function (a, b) {
return a.concat(b);
})
.forEach(function (x, i, v) {
if (x && nondupes.countItem(x) == 0) {
nondupes.push(x)
} else if (x && nondupes.countItem(x) >= 1) {
s.getRange(i + 1, 3)
.setBackground('red');
}
});
});
}
This is the sheet I tested it on and you can see starting from sheet 3 that the results were random. The file should only have the headers as duplicates.
https://docs.google.com/spreadsheets/d/1naYaxR0f_wGsRP4-nBRZfAJ9ZqxpyA1uqQ9Uz5xvwNs/edit#gid=1044860260

Related

Function that loops through the sheet

I'm trying to make Apps Script macro that will do following:
Check if checkmark (cell value = TRUE/FALSE) is ticked in column A
If it's ticked, increase value of cell in column H
Repeat until reached the last row.
After a little bit of digging around in documentation, I came up with this, and from my understanding it should be working, but since I'm asking a question here, it obviously does not. Script does get executed without errors, but it changes nothing on the sheet.
function increment() {
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var EndRow = ss.getLastRow();
for (var i = 2; i <= EndRow; i++) {
var Cell = ss.getRange(i,8);
var CheckmarkCell = ss.getRange(i,1);
if (CheckmarkCell.getValue() == 'TRUE') {
ss.Cell.setValue(ss.Cell.getValue() + 1);
}
}
}
Anyone has any idea, what's wrong here?
See if this helps?
function increMentWhenTrue() {
const sh = SpreadsheetApp.getActive().getSheetByName('Sheet1').getDataRange().offset(2, 0)
const values = sh.getValues().map(r => {
r[7] = (r[0]) ? (r[7]) ? r[7]+1 : 1 : r[7];
return r;
})
sh.setValues(values);
}
Change the sheet name in the first line to suit your requirements.
Same code with if-else statements
function increMentWhenTrue() {
const sh = SpreadsheetApp.getActive().getSheetByName('Blad1').getDataRange().offset(2, 0)
const values = sh.getValues().map(r => {
if (r[0]) {
if (r[7]) {
r[7] = r[7] + 1;
} else {
r[7] = 1;
}
} else {
r[7] = r[7];
}
return r;
})
sh.setValues(values);
}

how do you apply code to all tabs on a google sheet

Novice at the google scripting ... so apologies ..
I have the below code that works ... however I have a further ten tabs that this needs to apply to ... is there a way of writing this so you don't have to reference each active sheet?
the idea is to hide rows and columns automatically if a certain value exists in them ...
function onOpen()
{
var s = SpreadsheetApp.getActive().getSheetByName('O4');
s.showRows(1, s.getMaxRows());
s.getRange('BQ:BQ')
.getValues()
.forEach( function (r, i) {
if (r[0] == 'Done')
s.hideRows(i + 1);
});
var b = SpreadsheetApp.getActive().getSheetByName('O4');
b.showColumns(1, b.getMaxColumns());
b.getRange('135:135')
.getValues()[0]
.forEach(function (r, i) {
if (r && r == 'N') b.hideColumns(i + 1)
});
var s = SpreadsheetApp.getActive().getSheetByName('OG3');
s.showRows(1, s.getMaxRows());
s.getRange('BQ:BQ')
.getValues()
.forEach( function (r, i) {
if (r[0] == 'Done')
s.hideRows(i + 1);
});
var b = SpreadsheetApp.getActive().getSheetByName('OG3');
b.showColumns(1, b.getMaxColumns());
b.getRange('135:135')
.getValues()[0]
.forEach(function (r, i) {
if (r && r == 'N') b.hideColumns(i + 1)
});
}
You can just loop over all of the tabs in your sheet using this snippet.
var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
sheets.forEach(function (sheet) {
callYourFunction(sheet)
})
If you need to on apply your code on particular sheets do this.
var sheets = ['SheetA', 'SheetB', 'SheetG', 'SheetH', 'SheetM']
for (var i = 0; i < sheets.length; i++) {
var sheetName = sheets[i]
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
if (sheet != null) {
callYourFunction(sheet)
}
}
Hope that helps

Google sheets Script to Hide Rows in Batch

I'm currently using this script to hide rows containing 0 on col K
function Hide() {
var s = SpreadsheetApp.getActive()
.getSheetByName('Sheet1');
s.getRange('K:K')
.getValues()
.forEach(function (r, i) {
if (r[0] !== '' && r[0].toString()
.charAt(0) == 0) s.hideRows(i + 1)
});
}
Which works perfect, the only thing is that here when I run the script, it hides row by row (now that I have a lot of rows it takes so much time).
Is there a way to change it to work in batch?
This is the script that makes the magic
function Hide() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Ventas");
var currentRange = ss.getRangeByName("RangeCalculation");
var rangeStart = currentRange.getRow();
var values = currentRange.getValues();
var index = 0, rows = 1;
var show = !(values[0][12] == "" );
for (var i = 1, length = values.length; i < length; i++) {
if (values[i][0] == 1 ) {
if (show) {
sheet.showRows(rangeStart + index, rows);
show = false;
index = i;
rows = 1;
} else
rows++;
} else {
if (show)
rows++;
else {
sheet.hideRows(rangeStart + index, rows);
show = true;
index = i;
rows = 1;
}
}
}
if (show)
sheet.showRows(rangeStart + index, rows);
else
sheet.hideRows(rangeStart + index, rows);
}
Instead of hideRows(rowIndex), use hideRows(rowIndex, numRows)
The first form use only one parameter rowIndex, the second use two parameters, rowIndex and numbRows.
Obviously, using the suggested method implies to review the logic of your script.

Automatic Date Script

I am trying to create a script to automatically advance dates in a spreadsheet by one. Is there any way to do this?
For example, in our spreadsheet, we may have 4 different dates that need to advance by one.
2/17/2017
2/14/2017
2/15/2017
2/18/2017.
Basically everytime I want to run this script, I want all dates in a spreadsheet to advance by one. Any help is appreciated!
I tested this with some financial data and it appears to work okay. It runs fairly fast.
If you want all of the dates in the spreadsheet then the first line of incrDate should be
var rng = SpreadsheetApp.getActiveSheet().getDataRange();
and I just went back and tested that as well.
This is the entire code for the code.gs file including menu. So you may want to doctor it up a bit.
I was a fun problem. Actually the hardest part was figuring out the isDate function.
Thanks
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu('Range Tools')
.addItem('Increment A Date by one day','incrDate')
.addToUi();
};
function incrDate()
{
var rng = SpreadsheetApp.getActiveRange(); // current selection
var rngA = rng.getValues();
if(rng.getNumRows() > 1 && rng.getNumColumns() > 1) // two dimension array
{
for(var i = 0; i < rngA.length; i++)
{
for(var j =0; j < rngA[i].length;j++)
{
if(isDate(rngA[i][j]))
{
rngA[i][j] = new Date(rngA[i][j].getTime() + (1 * 86400000));
}
}
}
}
if(rng.getNumRows() > 1 && rng.getNumColumns() == 1) //single column
{
for(var i = 0; i < rngA.length ; i++)
{
if(isDate(rngA[i][0]))
{
rngA[i][0] = new Date(rngA[i][0].getTime() + (1 * 86400000));
}
}
}
if(rng.getNumRows() == 1 && rng.getNumColumns() > 1)//single row
{
for(var i = 0; i < rngA[0].length ; i++)
{
if(isDate(rngA[0][i]))
{
rngA[0][i] = new Date(rngA[0][i].getTime() + (1 * 86400000));
}
}
}
if(rng.getNumRows() == 1 && rng.getNumColumns() == 1) //single cell
{
if(isDate(rngA[0][0]))
{
rngA[0][0] = new Date(rngA[0][0].getTime() + (1 * 86400000));
}
}
rng.setValues(rngA);
}
function isDate (x)
{
return (null != x) && !isNaN(x) && ("undefined" !== typeof x.getDate);
}

Use formula inside script

I would like to use a formula inside a custom function, like this for example:
function myFunction(range, value) {
var countNumber = COUNTIF(range; value); // COUNTIF is a formula that can be used in the spreadsheet
if (countNumber > 0) {
return "RESULT";
} else {
return "OTHER RESULT";
}
}
And then:
=MYFUNCTION(A1:A5,"VALUETOTEST")
I would like to simplify a huge formula:
Something like:
=IF(SUM(COUNTIFS(G182:G186;"ERROR";H182:H186;"62");COUNTIFS(G182:G186;"ERROR";H182:H186;"ALL"))>0;"ERRO";IF(SUM(COUNTIFS(G182:G186;"RETEST";H182:H186;"62");COUNTIFS(G182:G186;"RETEST";H182:H186;"TODOS"))>0;"RETEST";IF(COUNTIF(G182:G186;"UNIMPLEMENTED")>0;"UNIMPLEMENTED";"SOLVED")))
You have three ways of performing these actions.
Add the Sheet Formulas to the sheet itself in the ranges that you need. Then read the data from the result cells (wherever you set it to write to) using your GAS Function. You can then perform further processing using the results.
Use your GAS function to write Sheet Formulas into your sheet. Then use more GAS to read that result and process the data. The method for this can be found here: https://developers.google.com/apps-script/reference/spreadsheet/range#setFormula(String)
You can create a Custom Sheet Formula using GAS that you then use in your sheet. GAS can then read that result and process the information. This will require some research into JS as a whole to know how to recreate, combine, and perform the operations that you need the data in the sheet to perform.
You can find a guide to make Custom Formulas here: https://developers.google.com/apps-script/guides/sheets/functions
And a guide to JS here: http://www.w3schools.com/js/default.asp
W3 Schools has a quite comprehensive guide to JS. GAS uses all native JS methods as it is a JS coding environment. Check the GAS Reference for more on GAS-specific methods that may perform what you need.
If what you need is to check conditions and/or iterate through rows, try something like this:
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var range = sheet.getRange(startRow, startColumn, numRows, numColumns);
var values = range.getValues(); //This is a 2D array; iterate appropriately
for (i = 0; i < values.length; i++) {
if (values[i] == conditionToCheck) {
//perform code..OR
//continue; <- This works to skip the row if the condition is met
} else {
//perform alternate code if condition is not met
}
}
}
As I mentioned, .getValues() creates a 2D array. If you need to iterate through columns and rows, you will need 2 for() loops like so:
for (i = 0; i < values.length; i++) { //iterates through the rows
for(j = 0; j < values[i].length; j++) { //iterates through the columns in that current row
It is important to mention how GAS handles 2D arrays. values[i][j] denotes how much i rows there are and j columns. You can visualize like so:
values = [[A1, B1, C1],[A2, B2, C2],[A3, B3, C3]]
This is an array of arrays where the outer array is an array of the rows, while the insides are an array of cell values by column in that row.
Custom functions in google apps script do not have access to spreadsheet function. You may try using this =IF(COUNTIF(A1:A5,"VALUETOTEST")>0,"RESULT","OTHER RESULT")
If there is a huge formula for result, try creating functions for the result
function result1() {
return "RESULT";
}
function result2() {
return "OTHER RESULT";
}
Then use this =IF(COUNTIF(A1:A5,"VALUETOTEST")>0,RESULT1(),RESULT2())
Try this - copy the below function in apps script, and use this as Formula =myFunction("G182:G186","H182:H186") remeber to ensclose the range with ' " ' because you will be passing the range as string, and note both the ranges must be of equal length.
function myFunction(aRange, bRange) {
var cond_1 = "ERROR";
var cond_2 = "62";
var cond_3 = "ALL";
var cond_4 = "RETEST";
var cond_5 = "TODOS";
var cond_6 = "UNIMPLEMENTED";
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var aRange = sheet.getRange(aRange);
var aValues = aRange.getValues();
var bRange = sheet.getRange(bRange);
var bValues = bRange.getValues();
var count = 0;
var tmplength = 0;
if (aValues.length != bValues.length) {
return "Range length does not Match";
}
for (i = 0; i < aValues.length; i++) {
if (aValues[i] == cond_1 && bValues[i] == cond_2) {
count += 1;
}
if (aValues[i] == cond_1 && bValues[i] == cond_3) {
count += 1;
}
if (count > 0) {
return "ERROR";
} else {
count = 0;
if (aValues[i] == cond_4 && bValues[i] == cond_2) {
count += 1;
}
if (aValues[i] == cond_4 && bValues[i] == cond_5) {
count += 1;
}
if (count > 0) {
return "RETEST";
} else {
count = 0;
if (aValues[i] == cond_6) {
count += 1;
}
if (count > 0) {
return "UNIMPLEMENTED";
} else {
return "SOLVED";
}
}
}
}
}
This is how I solved my problem. I thank to people who helped me to reach this result!
// Like COUNTIFS
var countConditionals = function(cells, condition1, condition2) {
var count = 0;
for (i = 0; i < cells.length; i++) {
if (cells[i][0] == condition1 && cells[i][1] == condition2) {
count++;
}
}
return count;
}
// Like COUNTIF
var countConditional = function(cells, condition) {
var count = 0;
for (i = 0; i < cells.length; i++) {
if (cells[i][0] == condition) {
count++;
}
}
return count;
}
//Whole Formula
function verificaStatus(cells, db) {
const ERROR = "ERROR";
const ALL = "ALL";
const RETEST = "RETEST";
const NOTYET = "UNIMPLEMENTADED";
const SOLVED = "SOLVED";
var countErrors = countConditionals(cells, ERROR, db);
var countErrorsAll = countConditionals(cells, ERROR, ALL);
var sumErrors = countErrors + countErrorsAll;
if (sumErrors > 0) {
return ERROR;
} else {
var retest = countConditionals(cells, RETEST, db);
var retestAll = countConditionals(cells, RETEST, db);
var sumRetest = retest + retestAll;
if (sumRetest > 0) {
return RETEST;
} else {
var countNonCreated = countConditional(cells, NOTYET);
if (countNonCreated > 0) {
return NOTYET;
}
}
}
return SOLVED;
}