Get range based on indexOf - javascript

Im trying to access a specific cell, in a row that corresponds to an index. So for index 5, go to cel F6 and do stuff. I’am getting the following error:
TypeError: isInColumnid is not a function, it is number. (line 23, file "testing4")
This is my code:
var isInColumnid = columntosearchid.indexOf(id)
var valuestoreplace = isInColumnid.getRange('!I:O')

I think you can save yourself a good amount of hassle on this by using the
math.range(start, end [, includeEnd]) syntax.
You will be able to pass your variables appropriately while also saving lines of confusion.

My obvious mistake was trying to get a range from a number, or applying a method to a result I’ve already gotten. So thinking a bit further, it makes more sense to look for the cell in the respective sheet and telling .getRange the exact coordinates by using the existing index as below.
var valuestoreplace = sheet2.getRange(isInColumnid+1, 9)

Related

How is data stored in arrays from getValue

In Google Sheets looking as part of a script to store data in a range to an array then to check whether a certain column has an "Y" in so I can loop through and store these columns in new arrays.
I have the following code but am getting this error - "TypeError: Cannot read property "0.0" from undefined."
var data = sheet.getRange("A6:U37").getValues;
if (data[20][i]=="Y"){
(The if code is generating the error)
Believe I am misunderstanding how the range is stored in the array causing the error any advice?
In the first line of code you provided, you are referencing the function getValues rather than actually calling it. In order to do so, you just have to modify the code as follows:
var data = sheet.getRange("A6:U37").getValues();
if (data[20][i]=="Y"){
Next time you have issues similar to this one, you can consider using logging or other debugging techniques in order to debug your script.

How can I getValue() of specific number of characters in a cell/range

I am trying to find a way to only getValue() of a specific number of characters in a cell/range. For example:
I'm looking at this data in a cell= 4/17/2019 10:15:48
I want to take that data and compare it to another data to determine if it is today or not. The issue is I don't care if it's 10:15:48. I just want to know if it was done on 4/17/2019.
Is it possible to getValue of the cell, but only take the first 9 characters instead of the entire cell data.
Thank you for your help in advance.
For example, it supposes that the value of 4/17/2019 10:15:48 is put in a cell "A1" of the active sheet. In this case, how about the following sample script? I thought that when the value retrieved by getValues() is a date object, the result you want cannot be retrieved. So in this modification, I used getDisplayValue() for retrieving the value.
Sample script:
var sheet = SpreadsheetApp.getActiveSheet();
var value = sheet.getRange("A1").getDisplayValue();
var res = value.substr(0, 9);
Logger.log(res)
Note:
If you want to retrieve values from several cells, you can also use getDisplayValues().
References:
getDisplayValue()
substr()
If I misunderstood your question, I apologize.

Google datatable sort columns as numbers

Using the google embed api and datatables to visualize my analytics. The problem is the columns don't resort correctly -- that is everything is treated as a string. I have this code:
gapi.client.analytics.data.ga.get(queryObj1).execute(function(results){
var myTable = new google.visualization.DataTable(results.dataTable);
....
This creates the myTable correctly and when I debug I see that the columns created have the correct type -- string or number. However when I check the values in results.dataTable (the object returned by the query) I notice that everything is a string -- as one might expect from an ajax call.
I found this thread about rewriting the sort function, but that seems a bit complex to me and if anything went wrong I'm not sure I'd be able to figure it out.
My approach is to iterate through the datatable and convert all the number columns to actual numbers.
function makeNumbers(results){
// make numbers actually numbers so they will sort properly
for(var c in results.cols){
// check if the column is a number type
if(results.cols[c].type === "number"){
// fix the values for that column is all the rows
for(var r in results.rows){
results.rows[r].c[c].v = +results.rows[r].c[c].v
}
}
}
return results;
}
This seems to work great and to my mind is much simpler than changing the sorting function.
Can anyone see a problem with this? Or a better way to do it?

How to find column max or min of a multidimensional javascript array using D3?

Is there a way to use d3.max or d3.min to find the maximum or minimum of a specific column in a multidimensional javascript array?
var arr = new Array();
for(var i=0;i<10;i++){
arr.push([i,Math.random()]);
}
From this code I'd like to do something like d3.max(arr[*][1]) (with a wildcard, or otherwise) to get the maximum value of the second column, but specifying an index seems to require designating a row first, and then a column, which seems to limit the d3.max routine to row operations only.
I know I can code a separate function to extract this information, but am wondering if there is something built into the D3 API or in Javascript that will do this.
I found one way to do this using the map method:
arr.map(function(x){return x[1];})
This will return an array of the items at index "1" in each mapped element. With this, applying the d3.max function works fine. I guess its the solution for now.

jQuery Id Selection and Dynamic Arrays

I have some code I'm struggling with. The good news is the code working as intended for a single instance; after some thought I've decided to feature multiple of these image selectors on a page. This works but the ugly approach of duplicating the code doesn't scale well (e.g. what if you want 50 of these on there?) The snag I've hit is how I can refer to a specific array. Is an array even an ideal solution for this?
The Objective
I have a series of images that a user may select from, up to X amount. The selected image ids are stored in an array and the image is added to a "selected images pool". This occurs by using an onClick for the slider, I obtain the Id from the element attributes. This is where I'm getting stuck.
var dataArray = $(this).closest("[id^=carousel]").data('array');
var slideCounter = $(this).closest("[id^=carousel]").data('counter');
slideCounter = dataArray.length;
The slideCounter returns the length of the string, not the array elements. How can I tell this code to refer to a particular array? See the fiddle for a better idea of the markup and code: jsFiddle
I have no doubt that there is a better approach. I'm relatively new to front end work, I'd appreciate any insights, I've burnt some brain cells on this, thanks!
From looking at your HTML, it looks like when you do this:
var dataArray = $(this).closest("[id^=carousel]").data('array');
what you're trying to do is to read the name of an array with .data() and then somehow turn that name (which is a string) into the array that's in your variable. My guess is that there's probably a better way to structure your code rather than putting javascript variable names in your HTML. I'd probably put a key name in the HTML and then store the arrays in an object where you can access them by that key name at any time.
Without trying to refactor your code, here's an idea for what you were trying to accomplish:
If selectedSlidesIdArray1 is a global variable, then you can do this:
var dataArray = window[$(this).closest("[id^=carousel]").data('array')];
Using the [stringVariable] notation on an object, lets you access a property by a literal string or a variable that contains a string. Since all global variables are also properties on the window object, you can do it this way for global variables.
If selectedSlidesIdArray1 is not a global variable, then you should probably put it in an object and then you can do this:
var dataArray = yourObj[$(this).closest("[id^=carousel]").data('array')];
Instead of trying to translate an arbitrary string into a JavaScript variable of the same name, why not just use another array? You can have nested arrays, which is to say an array of arrays.
Thus, instead of selectedSlidesIdArray1, selectedSlidesIdArray2, etc., you would have one selectedSlidesIdArray with sub-arrays, which you could then pull the index for using a data attribute.

Categories

Resources