AppScript issues with executing callback functions - javascript

Sorry if this has been asked before, I'm newish to programming so I'm not sure how exactly to define this issue. So I've been trying to code an AppScript extension to find what geographical area a given point lies in. My function names isInsideWrapper successfully identifies whether or not the given point is inside the area. However, when I try to then write a function to search for which area it's in, isInsideWrapper returns false instead of true for the area the point is in. Here's how the Google sheet looks, with the output for areaSeach and isInsideWrapper shown, as well as some of my code:
function areaFinder (polyRange,pt) {
let arr = []
for (let i = 0; i<polyRange.length; i++){
arr.push(isInsideWrapper(polyRange[i],pt))
}
return arr
}
Google Sheet
I've tried mapping the equation, printing the output of isInsideWrapper on just polyRange1, the area where it should be true (that still comes out as false when done through areaFinder). When I print just polyRange1, it's the same exact thing as the input. So I'm really lost as to why it's returning false. If needed, I can post details of the other callback functions.

Related

Javascript Nested For Loop Array has hidden value

I have been using javascript to run some simple code that uses the leaflet.js library (in this instance, the particular function is mymap.layerPointToLatLng(), here is the documentation ).
I want to use some data of the form [[[x,y]],[[x,y]],...] (the format is particular for how leaflet.js handles polygons and multipolygons, this kind of format is what I have called 'dim=2'). I need to convert it into latitudes and longitudes (hence the previously mentioned function). Because I do this a lot, I wrote a function to do it for me:
/*-Formatting functions*/
function coordconvert(mapvar,array,dim){
var temp1in=[],tempout1=[],temp2out=[];
if(dim===1){
for(i=0;i<array.length;i++){
tempout1[i]=mapvar.layerPointToLatLng(array[i]);
}
return tempout1;
}
else if(dim===2){
for(i=0;i<array.length;i++){
temp1in=array[i];//each polygon
console.log(i);
console.log(temp1in);
console.log(tempout1);
//tempout1=[];//why do I need this line?
for(j=0;j<temp1in.length;j++){
console.log(tempout1);
var checking = mapvar.layerPointToLatLng(temp1in[j]);
tempout1[j]=mapvar.layerPointToLatLng(temp1in[j]);//each vertex of polygon
console.log(j);
console.log(tempout1);
console.log(checking);
}
temp2out[i]=tempout1;//array of polygons
}
return temp2out;
}
else{
console.log("Unable to process coordinate conversion on array");
return
}
}
However, the 'if(dim===2)' section does not appear to be working correctly, hence all of the console.log lines.
In particular, the assignment tempout1[j]=mapvar.layerPointToLatLng(temp1in[j]); only appears to be working if I uncomment the line //tempout1=[];//why do I need this line?.
Using the console.log's, and viewed using Google Chrome, I get the following outputs:
First few iterations of loop
Final iteration of the loop
So as can be seen, The value (an object) of the final iteration is being included in the array of tempout1 before anything has been assigned to it (I have tried removing the 'if(dim===1)' section, and renaming the tempout1 variable, with no luck), and is being hidden by Chrome(?!?!?!); The value is not being over-written during the loop (as can be seen by the comparison of console.log(checking) and console.log(tempout1).
Why do I have to scrub the variable each time before the nested loop runs? And why is the value from the final iteration getting in before anything happens?
Turns out the solution was quite simple - I wasn't declaring the loop variables properly (i.e. for (var i=o;i<array.length;i++), rather than for (i=o;i<array.length;i++)) I'm not sure how this explains the behaviour of the Google Chrome console, but everything seemed much more stable afterwards.

Getting “This operation is not supported from a callback function” error when transposing array

First apologies for this question but GAS is a new syntax to me.
I would like to do something that is simple in VBA syntax, but not in GAS.
I want to transpose one named range to another named range (e.g. from 4 rows to 4 columns) on different worksheets. There are examples on the web that use .getDataRange() but I wish to use a fixed named range, whether there is data contained within or not.
The syntax I am playing around with is the following:
var wksInput = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('New');
var wksDB = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Database');
var intDBNewRow = wksDB.getLastRow() + 1;
wksDB.getRange(intDBNewRow, wksDB.getRange('dbContacts').getColumn(), 1,wksDB.getRange('dbContacts').getNumColumns()).setValues(transposeRange(wksInput.getRange('frmVanContacts').getValues()));
Taking 4 rows from a fixed point on an input form worksheet (wksInput) to 4 columns on the last row of a recordset/database worksheet (wksDB)
Where transposeRange is:
function transposeRange(a)
{
return Object.keys(a[0]).map(function (c) { return a.map(function (r) { return r[c]; }); });
}
The error is:
This operation is not supported from a callback function.
What is wrong here?
Answer
The problem isn't with the transpose function, the problem is that the Google Apps Script debugger can't do step into callbacks by design, so when debugging your code, instead of doing a debug step into, put a breakpoint before and after the call of the transpose function in order to make the debugger to skip that call.
References
In Google Apps Script Issues there is a issue about this -> Issue 4662: Debug breakpoint within the function body of an Array.map() call causes a server error that has the following comment from a Googler
From https://code.google.com/p/google-apps-script-issues/issues/detail?id=4662#c3
This is currently working as intended -- we do not support debugger
breakpoints withing callback functions. This issue will therefore be
marked as a Feature request.
A related question with no answers yet but with informative comments that help me to find the above reference
Debugger fails with "This operation is not supported from a callback function."

Debugger fails with "This operation is not supported from a callback function." [duplicate]

First apologies for this question but GAS is a new syntax to me.
I would like to do something that is simple in VBA syntax, but not in GAS.
I want to transpose one named range to another named range (e.g. from 4 rows to 4 columns) on different worksheets. There are examples on the web that use .getDataRange() but I wish to use a fixed named range, whether there is data contained within or not.
The syntax I am playing around with is the following:
var wksInput = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('New');
var wksDB = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Database');
var intDBNewRow = wksDB.getLastRow() + 1;
wksDB.getRange(intDBNewRow, wksDB.getRange('dbContacts').getColumn(), 1,wksDB.getRange('dbContacts').getNumColumns()).setValues(transposeRange(wksInput.getRange('frmVanContacts').getValues()));
Taking 4 rows from a fixed point on an input form worksheet (wksInput) to 4 columns on the last row of a recordset/database worksheet (wksDB)
Where transposeRange is:
function transposeRange(a)
{
return Object.keys(a[0]).map(function (c) { return a.map(function (r) { return r[c]; }); });
}
The error is:
This operation is not supported from a callback function.
What is wrong here?
Answer
The problem isn't with the transpose function, the problem is that the Google Apps Script debugger can't do step into callbacks by design, so when debugging your code, instead of doing a debug step into, put a breakpoint before and after the call of the transpose function in order to make the debugger to skip that call.
References
In Google Apps Script Issues there is a issue about this -> Issue 4662: Debug breakpoint within the function body of an Array.map() call causes a server error that has the following comment from a Googler
From https://code.google.com/p/google-apps-script-issues/issues/detail?id=4662#c3
This is currently working as intended -- we do not support debugger
breakpoints withing callback functions. This issue will therefore be
marked as a Feature request.
A related question with no answers yet but with informative comments that help me to find the above reference
Debugger fails with "This operation is not supported from a callback function."

Google Spreadsheet Cycler

First of all, I know I've posted something similar to this previously, but i got one response that I couldn't understand and that was it. So, I am trying to make an app script for google sheets to be able to increase the value of the number in a specific cell every weekday. I already know how to set up the time-based trigger for the timing, so all I need is the actual code. I know very minimal about coding, and this is the best I could come up with:
function onEdit(e) {
var range = e.range
var sheet = e.range.getSheet();
sheet.getRange(range.getRow(), 1).setValue(e.(e + 1));
}
When I try to run it, it says:
TypeError: Cannot read property "range" from undefined.(line 2,file"")
Any ideas on how to fix this would be greatly appreciated! But please, please put it in terms that a noob can understand!
The code you have now is on the right track but with a bit of a misunderstanding.
The onEdit trigger IS a trigger, but it is not a timebased one. onEdit is called every time a cell is edited in the spreadsheet. If you are calling this function from a time-based trigger, it will not pass an event object (e) like it would if it were triggered by an edit, which is why you are getting your error.
Instead, modify your time based trigger to call a different function that doesn't depend on an event object.
My understanding is that it is a static cell that you are incrementing (what I mean by this is that it will always be in the same place) so my suggestion is to grab the spreadsheet, grab the appropriate sheet, grab the cell, and edit its contents, similar to what you are already doing but more manual. In code, it would look like this :
function incrementCell(){
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('SheetName');
var value = sheet.getRange('B2');
value.setValue(value.getValue() + 1);
}
Simply substitute your sheets name and your cells A1 Notation location into the code and point your time-drive trigger to this function and it should work for you.

Custom function in Google spreadsheet to calculate disagreement between two people

I'd like to create a custom function in Google spreadsheet (so javascript is is) to measure the level of disagreement between people who answered a survey.
Here is what I wrote:
function disagree(DISAG,otherMember) {
var otherMember = '';
var indivDisag = DISAG.localeCompare(otherMember);
return Math.abs(indivDisag);
}
The localCompare should allow me to easily get 0 (if similar) or 1 (if different), but I keep getting 1. What did I mess up?
With this basic formula, I can check the disagreement of one person with another one.
edited for clarification
Thanks for your help
Well, considering that you always set otherMember to '', I think it is expected that there's always a difference. Why don't you try:
function disagree(DISAG, otherMember) {
return Math.abs(DISAG.localeCompare(otherMember));
}

Categories

Resources