How can I use a custom function with FILTER? - google-apps-script

I have a custom function defined that extracts part of an address from a string:
/*
* Return the number preceding 'N' in an address
* '445 N 400 E' => '445'
* '1083 E 500 N' => '500'
*/
function NorthAddress(address) {
if (!address) return null;
else {
var North = new RegExp('([0-9]+)[\\s]+N');
var match = address.match(North);
if (match && match.length >= 2) {
return match[1];
}
return null;
}
}
I want to use this function as one of the conditions in a call to FILTER(...) in the spreadsheet where I have these addresses stored:
=FILTER('Sheet 1'!A:A, NorthAddress('Sheet 1'!B:B) >= 450))
But when I call NorthAddress like this, it gets an array of all the values in column B and I can't for the life of me find any documentation as to how I need to handle that. The most obvious way (to me) doesn't seem to work: iterate over the array calling NorthAddress on each value, and return an array of the results.
What does my function need to return for FILTER to work as expected?

When a custom function is called passing a multi-cell range, it receives a matrix of values (2d array), it's doesn't matter if the range is a single column or a single row, it's always a matrix. And you should return a matrix as well.
Anyway, I would not use a custom function to this, as there is already the native spreadsheet formulas: RegexMatch, RegexExtract and RegexReplace formulas. To get the "if match" behavior, just wrap them in a IfError formula.

It doesn't work because address is, if you pass only one cell as arg a string, a range, a matrix of string.
So you return a string, FILTER use a boolean array to filter data, so the condition of your filter is string < number.
You just have to convert the string to a number when you returning a value
/*
* Return the number preceding 'N' in an address
* '445 N 400 E' => '445'
* '1083 E 500 N' => '500'
*/
function NorthAddress(address) {
if(typeof address == "string"){
if (!address) return "#N/A";
else {
var North = new RegExp('([0-9]+)[\\s]+N');
var match = address.match(North);
if (match && match.length >= 2) {
return parseInt(match[1]);
}
return "#N/A";
}
} else {
var matrix = new Array();
for(var i = 0; i<address.length; i++){
matrix[i] = new Array();
for(var j = 0; j<address[i].length; j++){
var North = new RegExp('([0-9]+)[\\s]+N');
var match = address[i][j].match(North);
if (match && match.length >= 2) {
matrix[i].push(parseInt(match[1]));
}
}
}
return matrix;
}
}
Hope this will help.

I will add this as an answer, because I found the custom function returns an error if numerical values are passed in the referenced cell or range when toString() is not invoked:
function NorthAddress(address) {
if (!address) return null;
else {
if (address.constructor == Array) {
var result = address;
}
else {
var result = [[address]];
}
var north = new RegExp('([0-9]+)[\\s]+N');
var match;
for (var i = 0; i < result.length; i++) {
for (var j = 0; j < result[0].length; j++) {
match = result[i][j].toString().match(north);
if (match && match.length >= 2) {
result[i][j] = parseInt(match[1]);
}
else {
result[i][j] = null;
}
}
}
return result;
}
}

Related

Importing Twilight Data to google sheets

I'm going to be honest, I know very little about any of this and the last time I did any form of programming was in high school 12 years ago.
I am needing to create a schedule for a low budget shoot, however, that is happening in August and I need to send out a daily schedule for the upcoming days as it changes.
I've been trying to work out how to potentially amend this so that it could include the other twilight times as well, but it keeps giving me an error:
// for an idiot, what am I doing wrong? Ideally it would be constructed in such a way that I can use it on future projects as well, placing a screen grab - it would be great if the formula can make reference to other cells and update as those cells update:
screengrab of google sheets
function SolarTimes(lat,long,date,type) {
var response = UrlFetchApp.fetch("https://api.sunrise-sunset.org/json?lat="+lat+"&lng="+long+"&date="+date);
var json = response.getContentText();
var data = JSON.parse(json);
var sunrise = data.results.sunrise;
var sunset = data.results.sunset;
var civil_dawn = data.results.civil_twilight_begin;
var civil_dusk = data.results.civil_twilight_end;
var nautical_dawn = data.results.nautical_twilight_begin;
var nautical_dusk = data.results.nautical_twilight_end;
var day_length = data.results.day_length;
{ if (type == "Sunrise")
return sunrise;
else if (type == "Sunset")
return sunset;
else if (type = "Civil_Dawn")
return civildawn;
else if (type == "Civil_Dusk")
return civildusk;
else if (type == "Nautical_Dawn")
return nauticaldawn;
else if (type == "Nautical_Dusk")
return nauticaldusk;
else
return day_length};
}
Here's an implementation that handles numeric dates and validates arguments.
/**
* Gets the sunrise or sunset time or day length at a location on a date.
*
* #param {36.7201600} latitude The north–south position to use.
* #param {-4.4203400} longitude The east-west position to use.
* #param {"sunrise"} type One of "all", "sunrise", "sunset", "civil_dawn", "civil_dusk", "nautical_dawn", "nautical_dusk" or "day_length".
* #param {D2} date Optional. Defaults to the current date.
* #return {String|String[][]} The requested time as a text string. With "all", an array of types and times.
* #customfunction
*/
function Daylight(latitude, longitude, type, date) {
// see https://stackoverflow.com/a/72675674/13045193
// note: api.sunrise-sunset.org/json does not handle polar night nor midnight sun correctly
'use strict';
const [lat, lng, key, dateString] = _validate(arguments);
const url = `https://api.sunrise-sunset.org/json?lat=${lat}&lng=${lng}&date=${dateString}`;
const { results, status } = JSON.parse(UrlFetchApp.fetch(url).getContentText());
if (key === 'all') {
return Object.keys(results).map(key => [key, results[key]]);
}
return results[key] ?? NaN;
/**
* Validates function arguments.
*/
function _validate(args) {
if (args.length < 3 || args.length > 4) {
throw new Error(`Wrong number of arguments to Daylight. Expected 3 or 4 arguments, but got ${args.length} arguments.`);
}
const lat = Number(latitude);
const lng = Number(longitude);
if (latitude === '' || Number.isNaN(lat) || lat > 90 || lat < -90 || longitude === '' || Number.isNaN(lng) || lng > 180 || lng < -180) {
throw new Error(`Daylight expected a numeric latitude [-90, 90] and longitude [-180, 180], but got the ${typeof latitude} '${latitude}' and the ${typeof longitude} '${longitude}'.`);
}
return [
lat,
lng,
type.toLowerCase().replace('dawn', 'twilight_begin').replace('dusk', 'twilight_end'),
_dateToISO8601(date),
];
}
/**
* Parses a date or string to an ISO8601 date string.
*/
function _dateToISO8601(date) {
if (date === '' || (date == null)) {
date = new Date();
}
if (typeof date === 'string') {
date = new Date(Date.parse(date));
}
if (Object.prototype.toString.call(date) !== '[object Date]') {
throw new Error(`Daylight expected a date, but '${date}' is a ${typeof date}.`);
}
return Utilities.formatDate(date, SpreadsheetApp.getActive().getSpreadsheetTimeZone(), 'yyyy-MM-dd');
}
}
The code looks more or less fine. But the cell contains the little error I think. It should be thusly:
=Solartimes($G$3,$H$3,text($B$6,"yyyy-mm-dd"),C6)
As for the code I'd propose to use switch/case for this case:
function SolarTimes(lat,long,date,type) {
var response = UrlFetchApp.fetch("https://api.sunrise-sunset.org/json?lat="+lat+"&lng="+long+"&date="+date);
var json = response.getContentText();
var {results} = JSON.parse(json);
switch (type.toLowerCase()) {
case ('sunrise'): return results.sunrise;
case ('sunset'): return results.sunset;
case ('civil_dawn'): return results.civil_twilight_begin;
case ('civil_dusk'): return results.civil_twilight_end;
case ('nautical_dawn'): return results.nautical_twilight_begin;
case ('nautical_dusk'): return results.nautical_twilight_end;
case ('day_length'): return results.day_length;
}
return '--';
}
It works about the the same way but looks cleaner.
Just in case. The line:
var {results} = JSON.parse(json);
is the same as:
var data = JSON.parse(json);
var results = data.results;
See:
Destructuring assignment

Doesn´t recognice as equal (==)

Can someone tell me why these variables marked with red are not recognized as equal (==).
Google Apps Script is Javascript-based. In Javascript, you can not compare two arrays using ==.
One method is to loop over both arrays and to check that the values are the same. For example you can include the function:
function compareArrays(array1, array2) {
for (var i = 0; i < array1.length; i++) {
if (array1[i] instanceof Array) {
if (!(array2[i] instanceof Array) || compareArrays(array1[i], array2[i]) == false) {
return false;
}
}
else if (array2[i] != array1[i]) {
return false;
}
}
return true;
}
And then update the line in your code from if (responsables == gestPor) { to if (compareArrays(responsables, gestPor)) {
For other methods of comparing arrays in Javascript, see this answer.
It is because you are comparing arrays. If you are just getting a single cell value, use getValue() instead of getValues()
To make things work, change these:
var gestPor = hojaActivador.getRange(i,13,1,1).getValues();
var responsables = hojaConMails.getRange(1,n,1,1).getValues();
to:
var gestPor = hojaActivador.getRange(i,13).getValue();
var responsables = hojaConMails.getRange(1,n).getValue();
Do these to all getValues() where you're only extracting 1 cell/value.
See difference below:

How to use a for loop with .createChoice in Google Apps Script to create a quiz from a sheet?

I am using Google Apps Script to generate Google Forms from a Sheet. Questions are in rows and question choices are in columns.
Here is a link to the Google sheet if needed.
It is a straightforward task when using .setChoiceValues(values)
if (questionType == 'CHOICE') {
var choicesForQuestion = [];
for (var j = 4; j < numberColumns; j++)
if (data[i][j] != "")
choicesForQuestion.push(data[i][j]);
form.addMultipleChoiceItem()
.setChoiceValues(choicesForQuestion);
}
However, when I try to use .createChoice(value, isCorrect), the parameters call for value to be a string and isCorrect to be Boolean.
An example without a loop looks like this:
var item = FormApp.getActiveForm().addCheckboxItem();
item.setTitle(data[3][1]);
// Set options and correct answers
item.setChoices([
item.createChoice("chocolate", true),
item.createChoice("vanilla", true),
item.createChoice("rum raisin", false),
item.createChoice("strawberry", true),
item.createChoice("mint", false)
]);
I can not figure out how to add the loop. After reading over other posts, I have tried the following:
if (questionType == 'CHOICE') {
var questionInfo = [];
for (var j = optionsCol; j < maxOptions + 1; j++)
if (data[i][j] != "")
questionInfo.push( form.createChoice(data[i][j], data[i][j + maxOptions]) );
form.addMultipleChoiceItem()
.setChoices(questionInfo);
}
optionsCol is the first column of questions options
maxOptions is how many options are allowed by the sheet (currently 5). The isCorrect information is 5 columns to the right.
However, this not working because the array questionsInfo is empty.
What is the best way to do this?
Probably your issue is related to the method you reference--Form#createChoice--not existing. You need to call MultipleChoiceItem#createChoice, by first creating the item:
/**
* #param {Form} formObj the Google Form Quiz being created
* #param {any[]} data a 1-D array of data for configuring a multiple-choice quiz question
* #param {number} index The index into `data` that specifies the first choice
* #param {number} numChoices The maximum possible number of choices for the new item
*/
function addMCItemToForm_(formObj, data, index, numChoices) {
if (!formObj || !data || !Array.isArray(data)
|| Array.isArray(data[0]) || data.length < (index + 2 * numChoices))
{
console.error({message: "Bad args given", hasForm: !!formObj, info: data,
optionIndex: index, numChoices: numChoices});
throw new Error("Bad arguments given to `addMCItemToForm_` (view on StackDriver)");
}
const title = data[1];
// Shallow-copy the desired half-open interval [index, index + numChoices).
const choices = data.slice(index, index + numChoices);
// Shallow-copy the associated true/false data.
const correctness = data.slice(index + numChoices, index + 2 * numChoices);
const hasAtLeastOneChoice = choices.some(function (c, i) {
return (c && typeof correctness[i] === 'boolean');
});
if (hasAtLeastOneChoice) {
const mc = formObj.addMultipleChoiceItem().setTitle(title);
// Remove empty/unspecified choices.
while (choices[choices.length - 1] === "") {
choices.pop();
}
// Convert to choices for this specific MultipleChoiceItem.
mc.setChoices(choices.map(function (choice, i) {
return mc.createChoice(choice, correctness[i]);
});
} else {
console.warn({message: "Skipped bad mc-item inputs", config: data,
choices: choices, correctness: correctness});
}
}
You would use the above function as described by its JSDoc - pass it a Google Form object instance to create the quiz item in, an array of the details for the question, and the description of the location of choice information within the details array. For example:
function foo() {
const form = FormApp.openById("some id");
const data = SpreadsheetApp.getActive().getSheetByName("Form Initializer")
.getSheetValues(/*row*/, /*col*/, /*numRows*/, /*numCols*/);
data.forEach(function (row) {
var qType = row[0];
...
if (qType === "CHOICE") {
addMCItemToForm_(form, row, optionColumn, numOptions);
} else if (qType === ...
...
}
References
Array#slice
Array#forEach
Array#map
Array#some
I am sure the above answer is very good and works but I am just a beginner and needed a more obvious (plodding) method. I am generating a form from a spreadsheet. Question types can include: short answer (text item), long answer (paragraph), drop down (list item), multiple choice, grid item, and checkbox questions, as well as sections.
I had to be able to randomize the input from the spreadsheet for multiple choice and sort the input for drop downs. I am only allowing one correct answer at this time.
The columns in the question building area of the spreadsheet are: question type, question, is it required, does it have points, hint, correct answer, and unlimited choice columns.
qShtArr: getDataRange of the entire sheet
corrAnsCol: index within the above of the column with the correct answer
begChoiceCol: index within the above of first column with choices
I hope this helps other less skilled coders.
/**
* Build array of choices. One may be identified as correct.
* I have not tried to handle multiple correct answers.
*/
function createChoices(make, qShtArr, r, action) {
// console.log('Begin createChoices - r: ', r);
let retObj = {}, choiceArr = [], corrArr = [], aChoice, numCol, hasCorr;
numCol = qShtArr[r].length - 1; // arrays start at zero
if ((qShtArr[r][corrAnsCol] != '') && (qShtArr[r][corrAnsCol] != null)) {
hasCorr = true;
choiceArr.push([qShtArr[r][corrAnsCol], true]);
for (let c = begChoiceCol ; c < numCol ; c++) {
aChoice = qShtArr[r][c];
if ((aChoice != '') && (aChoice != null)) { /* skip all blank elements */
choiceArr.push([aChoice, false]);
}
} //end for loop for multiple choice options
} else {
hasCorr = false;
for (let c = begChoiceCol ; c < numCol ; c++) {
aChoice = qShtArr[r][c];
if ((aChoice != '') && (aChoice != null)) { /* skip all blank elements */
choiceArr.push(aChoice);
}
} //end for loop for multiple choice options
}
if (action == 'random')
choiceArr = shuffleArrayOrder(choiceArr);
if (action == 'sort')
choiceArr.sort();
console.log('choiceArr: ', JSON.stringify(choiceArr) );
let choices = [], correctArr = [] ;
if (hasCorr) {
for ( let i = 0 ; i < choiceArr.length ; i++ ) {
choices.push(choiceArr[i][0]);
// console.log('choices: ', JSON.stringify(choices) );
correctArr.push(choiceArr[i][1]);
// console.log('correctArr: ', JSON.stringify(correctArr) );
}
make.setChoices(choices.map(function (choice, i) {
return make.createChoice(choice, correctArr[i]);
}));
} else { // no correct answer
if (action == 'columns' ) {
make.setColumns(choiceArr);
} else {
make.setChoices(choiceArr.map(function (choice, i) {
return make.createChoice(choice);
}));
}
}
}

Can't find a match

I have a nested arrays with pairs of numbers:
_open = [[8,15], [9,16]];
from which i want to find a match using ArrayUtilities.findMatchIndex but it always returns -1 when looking for an element array. For example:
ArrayUtilities.findMatchIndex(_open, [8, 15])
I'm wondering if it is possible for AS3 to compare arrays, because comparing other types (strings, numbers, etc) just work fine
Here's findMatchIndex():
public static function findMatchIndex(aArray:Array, oElement:Object, ...rest):Number {
var nStartingIndex:Number = 0;
var bPartialMatch:Boolean = false;
if(typeof rest[0] == "number") {
nStartingIndex = rest[0];
}
else if(typeof rest[1] == "number") {
nStartingIndex = rest[1];
}
if(typeof rest[0] == "boolean") {
bPartialMatch = rest[0];
}
var bMatch:Boolean = false;
for(var i:Number = nStartingIndex; i < aArray.length; i++) {
if(bPartialMatch) {
bMatch = (aArray[i].indexOf(oElement) != -1);
}
else {
bMatch = (aArray[i] == oElement);
}
if(bMatch) {
return i;
}
}
return -1;
}
Comparing other types (strings, numbers, etc) works fine, because they are so-called primitives, and are compared by values. Arrays, though, are objects, therefore they are compared by reference. Basically it means that [8,15] != [8,15].
One way around it is replacing this line...
else {
bMatch = (aArray[i] == oElement);
}
... with something like this ...
else {
bMatch = compareElements(aArray[i], oElement);
}
... where compareElements will try to check its arguments' types first, and if they're objects, will compare their values.

AS3: indexOf() sub-array in a multi-dimensional array

var asdf:Array = [ [1,1] ];
trace( asdf.indexOf( [1,1] ) ); // -1
Why can't indexOf() find the [1,1] array?
Here is a little function I wrote a while ago that works great. I included a lot of comments and an example search/function to output the results.
// set up a multidimensional array that contains some data
var myArray:Array = new Array();
myArray.push(["granola","people... are great"," 4 ","10"]);
myArray.push(["bill","orangutan","buster","keaton"]);
myArray.push(["steve","gates","24","yes, sometimes"]);
myArray.push(["help","dave","jobs","hal"]);
// here we set up some properties on the array object to hold our search string and our results
myArray.myTarget = "steve";
myArray.myResults = [];
// now we call the search
myArray.forEach(multiSearch);
// this is the function that does all the heavy lifting....
function multiSearch(element:*, index:int, array:Array)
{
// see if we have a match in this array and pass back its index
for(var i:* in element)
{
if( element[i].indexOf( array.myTarget ) > -1 )
{
var tempArray:Array = array.myResults;
tempArray.push([index,i]);
array.myResults = tempArray;
}
}
}
// -------------------------------------------------------------------------------
// all the code below is OPTIONAL... it is just to show our results
// in the output window in Flash so you know it worked....
var printArray:Array = myArray.myResults;
for(var i:* in printArray)
{
trace("TARGET FOUND #: "+printArray[i][0]+", "+printArray[i][1]+" = "+myArray[ printArray[i][0] ][ printArray[i][1] ]);
}
// -------------------------------------------------------------------------------
It fails because when you do a [x,y] you are creating a new array, adsf contains one array and indexOf search for another one.
try:
trace([1,1] == [1,1]);
You will see that it prints false, since array are compare by reference.
One quick indexOf function, arrange it to suit your needs:
function isElmEquals(e1:*, e2:*):Boolean {
return (e1==e2);
}
function isArrayEquals(a1:Array, a2:Array):Boolean {
if (a1==a2)
return true;
if ((a1==null) || (a2==null)) {
return false;
}
if (a1.length!=a2.length)
return false;
for (var i:int=0;i<a1.length;i++){
if (!isElmEquals(a1[i], a2[i]))
return false;
}
return true;
}
function indexOf(value:Array, into:Array):int{
var i:int = -1;
into.some(
function(item:*, index:int, array:Array):Boolean {
if (isArrayEquals(item as Array, value)) {
i = index;
return true;
}
return false;
}
);
return i;
}
var i:int=indexOf([1,1], [[-1,1], [0,1], [1,1], [1,-1]]);
trace(i);
var j:int=indexOf([1,2], [[-1,1], [0,1], [1,1], [1,-1]]);
trace(j);
this works. probably because the inner array is typed.
var qwer:Array = [1,1];
var asdf:Array = [qwer];
trace( asdf.indexOf( qwer ) ); // 0