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;
}
Related
I have a simple column in column A in a sheet by name Sno. (serial number). I am trying to read the column and identify -
-if there are any empty cells from first cell in the column to the last filled row and
-if the values which are present are numbers only
This will help me do 2 validations, to identify empty cells in between the cell values, like if there are 1 - 200 numbers entered then in between there are no misses in the series and if the values which are present all numbers
I tried the below to check that but not getting it right-
unction siteShieldmaps() {
SS = SpreadsheetApp.getActiveSpreadsheet();
var SS_m = SS.getSheetByName("Cleanup sheet");
var LAST_ROW = SS_m.getLastRow();
console.log(LAST_ROW);
var Sno_values = SS_m.getRange(`A1:A${LAST_ROW}`).getDisplayValues().toString();
console.log(typeof Sno_values);
var result = isNumberOrEmpty(Sno_values);
console.log(result);
}
function isNumberOrEmpty(array) {
var result = [];
for (var i = 0; i < array.length; i++) {
if (array[i] === "") {
result.push("empty");
} else if (!isNaN(array[i])) {
result.push("number");
} else {
result.push("not a number");
}
}
return result;
}
Please guide
Adding to the comment of #TheWizEd, one of the issues of the code is how the array has been called. I made other changes in the code to make sure that both validations are completed.
Make sure if there are any empty cells.
Make sure that all the values are numbers.
Here is the table I made for testing:
Here is the sample code:
function siteShieldmaps() {
ss = SpreadsheetApp.getActiveSpreadsheet();
let ss_m = ss.getSheetByName("Sheet1");
let last_row = ss_m.getLastRow();
console.log(last_row);
// change how the range is call from "A1:A${LAST_ROW} to "2,1,last_row"
// The range "2,1,last_row" will exclude the "A1" cell
// Also, I change "getDisplayValues().toString();" to "getValues()"
// if you keep "getDisplayValues().toString();"
//it will show some cells as not number when they are
let sno_values = ss_m.getRange(2,1,last_row).getValues();
let result = isNumberOrEmpty(sno_values);
console.log(result);
}
function isNumberOrEmpty(array) {
let result = [];
for (let i = 0; i < array.length; i++) {
// create the variable row instead of using array[i][0]
// so I use row[0] in the if statement
let row = array[i]
if (row[0] === "") {
result.push("empty");
} else if (!isNaN(row[0])) {
result.push("number");
} else {
result.push("not a number");
}
}
return result;
}
And the result will be:
I would really comment instead of posting but I don't have enough reputation.
In this line of code you're not actually getting a 2-D array but a string.
var Sno_values = SS_m.getRange(`A1:A${LAST_ROW}`).getDisplayValues().toString();
toString()
It should be just
var Sno_values = SS_m.getRange(`A1:A${LAST_ROW}`).getDisplayValues()
Besides, as TheWizEd remarked you are working with a 2D array, so your function should be:
function isNumberOrEmpty(array) {
var result = [];
for (var i = 0; i < array.length; i++) {
if (array[i][0] === "") {
result.push("empty");
} else if (!isNaN(array[i][0])) {
result.push("number");
} else {
result.push("not a number");
}
}
return result;
}
I'm trying to get a SCORESHEET to populate from a REPORTSHEET, using a REFERENCESHEET to collate search terms and destination cells.
The script I'm running is as below. The idea is that the script finds searchDate's in the REFERENCESHEET and uses them to locate data columns in the REPORTSHEET:
function superAuto() {
var report = SpreadsheetApp.openById('REPORTSHEET');
var reportData = report.getDataRange().getValues();
var reference = SpreadsheetApp.openById('REFERENCESHEET');
var referenceData = reference.getDataRange().getValues();
var scorecard = SpreadsheetApp.openById('SCORESHEET');
var scorecardData = scorecard.getDataRange().getValues();
var tExpenses = "Total Expenses";
for(n=0;n<referenceData.length;++n){
var searchDate = referenceData[n][0] ;
Logger.log (searchDate)
}
var column = columnfinder(searchDate);
for (var a = 0; a < referenceData.length; a++) {
var refRow = referenceData[a];
for (var i = 0; i < reportData.length; i++) {
var row = reportData[i];
if (row[0] == tExpenses && refRow[0] == searchDate) {
scorecard.getRange(refRow[5]).setValue(row[column]);
}
}
}
}
function columnfinder(find) {
var report = SpreadsheetApp.openById('REPORTSHEET');
var reportData = report.getDataRange().getValues();
var reference = SpreadsheetApp.openById('REFERENCESHEET');
var referenceData = reference.getDataRange().getValues();
for(var j=0, jLen=reportData.length; j<jLen; j++) {
for(var k=0, kLen=reportData[0].length; k<kLen; k++) {
if(find == reportData[j][k]) {
Logger.log(k);
return (k);}
}
}
}
Broadly speaking, the code works, as if I define searchDate as one of the terms I'm looking for (e.g. Jan-21) it all works fine. The issue is that it doesn't seem to be doing so when finding multiple search terms - and therefore populating multiple rows - as per:
for(n=0;n<referenceData.length;++n){
var searchDate = referenceData[n][0] ;
Logger.log (searchDate)
}
The log tells me that it's finding searchDate's in the REFERENCESHEET, but it's not able to run them through function columnfinder (I get no logs for the second logger).
I suspect the answer lay somewhere in an earlier great answer I received to an earlier version of this idea - How to return multiple column values for setValue - but I've not been able to make it fit. Any thoughts?
EDIT: Please find a sample REFERENCESHEET & REPORTSHEET for more info:
The log tells me that it's finding searchDate's in the REFERENCESHEET,
but it's not able to run them through function columnfinder (I get no
logs for the second logger)
You don't execute columnfinder inside the for loop.
Try this:
for(n=0;n<referenceData.length;++n){
var searchDate = referenceData[n][0] ;
Logger.log(searchDate);
columnfinder(searchDate); // modified code
}
and you will get both logs.
Sorry if I misunderstood your question.
You need to use have it assigned to array since you are returning possible multiple columns/dates:
function superAuto() {
var report = SpreadsheetApp.openById('REPORTSHEET');
var reportData = report.getDataRange().getValues();
var reference = SpreadsheetApp.openById('REFERENCESHEET');
var referenceData = reference.getDataRange().getValues();
var scorecard = SpreadsheetApp.openById('SCORESHEET');
var scorecardData = scorecard.getDataRange().getValues();
var tExpenses = "Total Expenses";
var searchDates = [];
for (n = 0; n < referenceData.length; ++n) {
searchDates.push(referenceData[n][0])
}
var columns = columnfinder(searchDates);
columns.forEach(function (column, index) {
referenceData.forEach(function (refRow) {
reportData.forEach(function (row) {
if (row[0] == tExpenses && refRow[0] == searchDates[index]) {
scorecard.getRange(refRow[5].toString()).setValue(row[column]);
}
});
});
});
}
function columnfinder(dates) {
var report = SpreadsheetApp.openById('REPORTSHEET');
var reportData = report.getDataRange().getValues();
var reference = SpreadsheetApp.openById('REFERENCESHEET');
var referenceData = reference.getDataRange().getValues();
var columns = [];
dates.forEach(function (date) {
reportData.forEach(function (row, i) {
row.forEach(function (col, i) {
if (date == reportData[i][j]) {
columns.push(reportData[i][j]);
}
});
});
});
return columns;
}
I changed some variables into proper variable names to avoid confusion.
Additionally, if it doesn't work, you might need to share a proper visualization of the data, or better yet, provide some sample sheet we can work on for us to be able to give you a better and tested answer.
Thanks Marios, that's twice in a week. Much appreciated.
Slight adaptation, in order for it to populate SCORECARD I needed to bring everything into the for loop, as below:
for(n=0;n<referenceData.length;++n){
var searchDate = referenceData[n][0] ;
Logger.log (searchDate)
var column = columnfinder(searchDate);
for (var a = 0; a < referenceData.length; a++) {
var refRow = referenceData[a];
for (var i = 0; i < reportData.length; i++) {
var row = reportData[i];
if (row[0] == tExpenses && refRow[0] == searchDate) {
scorecard.getRange(refRow[5]).setValue(row[column]);
}
}
}
}
I have been working on a new project and I need to create new sheets based on cell value.
function ign_list() {
var nRange = SpreadsheetApp.getActive().getRangeByName("player")
return nRange.getValues()
}
function sheet_list() {
var out = new Array()
var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
for (var i=0 ; i<sheets.length ; i++) out.push( [ sheets[i].getName() ] )
return out
}
function new_sheet() {
var ui = SpreadsheetApp.getUi();
var sheet_names = sheet_list();
var ign_names = ign_list();
for (var i=0; i<ign_names.length; i++){
for (var n=0; n<sheet_names.length; n++) {
if (ign_names[i] !="") {
if (ign_names[i] == sheet_names[n]) {
ui.alert(ign_names[i]+" Equal "+sheet_names[n])
} else {
ui.alert(ign_names[i]+" Not Equal "+sheet_names[n])
}
}
}
}
}
I have a sheet called Raw Stats and in the range, I have a cell with the value Raw Stats but when comparing it says is not equal.
is there another way of comparing string?
Using indexOf
for (var i=0; i<ign_names.length; i++){
if (sheet_names.indexOf(ign_names[i][0]) != -1) {
SpreadsheetApp.getActiveSpreadsheet().insertSheet().setName(ign_names[i][0]);
} else {
}
}
Always returning -1 even when as the same value
You are using the getValues() method. That returns an array of rows.
Even if you range is compromised of only one column, the result of that would be: [[A1],[A2],[A3],...]
You can fix this by flattening the array or by accounting for this with ign_names[i][0] instead.
Hope this helps
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);
}
I found the below google-app-script online which finds duplicates in the specified range of rows in a google spreadsheet. I need to edit this script in such a way that if the duplicates occur more than 2 times it should show those values.
This is how the script looks like:
function dups(rows) {
var values = {};
var duplicates = [];
for (var i = 0; i < rows.length; i++) {
var value = rows[i][0];
if (values[value] !== undefined && duplicates.indexOf(value) == -1) {
duplicates.push(value);
} else {
values[value] = true
}
}
return duplicates;
}
For example with this script if i type =dups(A1:A30)in any cell i get the list of unique values which are repeated more than once . But i want values which are repeated more than twice.
Thanks in advance,
drc
With triples unfortunately you can't just to a (simple) indexOf so the most efficient way is to count occurrences and stop when we hit two.
function trips(rows) {
var output = [];
var appearances;
for (var row = 0; row < rows.length-2; row++) {
var value = rows[row][0];
if (output.indexOf(value) > -1) {
continue;
}
appearances = 0;
for (var row2 = row + 1; row2 < rows.length; row2++) {
if (value === rows[row2][0]) {
appearances += 1;
}
if (appearances >= 2) {
output.push(value);
break;
}
}
}
return output;
}