Values from a Range not coming up as an array - google-apps-script

I am unable to obtain an array from a getRange.getValues call to some data on a sheet, i.e. no brackets []. If I just create an array in the code I am OK. I am trying to use indexOf to locate a specific date. In my real code the date to match is also plucked from the sheet.
In the following test code the vars are global and not shown here.
function testArray() {
var rowTwoDates = playgroundSheet.getRange(2, 59, 1, 5).getValues();
var strippedRowTwoDates = rowTwoDates[0].map(stripTime);
Logger.log('array: ' + strippedRowTwoDates);
Logger.log('indexOf: ' + strippedRowTwoDates[0].indexOf("06/09/2022"));
var scores = [10, 20, 30, 10, 40, 20];
Logger.log(scores); // 2
Logger.log(scores.indexOf(30)); // 2
}
9:38:23 AM Notice Execution started
9:38:24 AM Info array: 06/07/2022,06/08/2022,06/09/2022,06/10/2022,06/11/2022
9:38:24 AM Info indexOf: -1
9:38:24 AM Info [10.0, 20.0, 30.0, 10.0, 40.0, 20.0]
9:38:24 AM Info 2.0
9:38:24 AM Notice Execution completed

You're only checking indexOf for the first value in the array here:
Logger.log('indexOf: ' + strippedRowTwoDates[0].indexOf("06/09/2022"));
I think you want to do this:
Logger.log('indexOf: ' + strippedRowTwoDates.indexOf("06/09/2022"));

Related

How to calculate time with Tabulator.js

I'm currently using Tabulator.js to get a table generated for my project.
In my project I receive information from firebase and I load it to my table which is designed by TABULATOR.js ( http://tabulator.info ).
I know how to calculat normal value in my table. But I would like to calculate time. For example in row 1 I get 00:45 (45min) and row 2 I get 1:10 the result row should give me 1:55.
I not really familiar with web codding that's why I request your help.
My code :
var table = new Tabulator("#editor", {
downloadConfig:{
columnGroups:false,
rowGroups:false,
},
height:360,
data: arrayFlight,
pagination:"local",
resizableColumns: false,
paginationSize:12,
columnVertAlign:"center",
columnHoriAlign:"center",
columns:[ //I show you only the requested column
{
title:"SINGLE PILOT FLIGHT TIME",
columns:[
{title:"SE", field:"seTime", align:"center", formatter:"datetime", formatterParams:{inputFormat:"hh:mm:ss", outputFormat:"hh:mm:ss", invalidPlaceholder:"-"}, sorter:false, headerSort:false, width:95, bottomCalc:"sum", bottomCalcParams:{precision:2}},
{title:"ME", field:"meTime", align:"center", formatter:"datetime", formatterParams:{inputFormat:"hh:mm:ss", outputFormat:"hh:mm:ss", invalidPlaceholder:"-"}, sorter:false, headerSort:false, width:95},
],
},
],
thanks for your help.
You would need to use a custom column calculation for this, something like this:
//define custom calculation function
var timeSum = function(values, data, calcParams){
//values - array of column values
//data - all table data
//calcParams - params passed from the column definition object
var time = 0;
values.forEach(function(value){
if(value){
//split time value into minutes and sum
var values = value.split(":");
time += parseInt(values[0]) * 60;
time += parseInt(values[1]);
}
});
//format sum as hour:minute
return Math.floor(time/60) + ":" + time%60;
};
Then you can assign it in your column definition:
{title:"SE", field:"seTime", bottomCalc:timeSum}

How to use Math.LN(x) in ActionScript 3

How to use Math.LN(x) in ActionScript 3?
I have a formula to convert:
17.867 * LN(x)-29.263
How to write in ActionScript 3? I'm confused about how to write it.
What I've tried :
var Kc:Number;
var value_x:Number;
function enterFrameHandler () : void
{
value_x=80;
Kc=(17.867)*Math.LN10(value_x) - 29.263;
value_Kc.text=String(Kc);
trace(Kc);
//y = 17.867ln(x) - 29.263//
}
enterFrameHandler();
Getting error :
Error : Scene 1, Layer 'Layer 1', Frame 1, Line 7, Column 19 1195 :
Attempted access of inaccessible method LN10 through a reference with
static type Class.
thanks for your replied #Organis may be directly show problem in my code
var Kc:Number;
var value_x:Number;
function enterFrameHandler () : void
{
value_x=80;
Kc=(17.867)*Math.LN10(value_x) - 29.263;
value_Kc.text=String(Kc);
trace(Kc);
//y = 17.867ln(x) - 29.263//
}
enterFrameHandler();
///get error Scene 1, Layer 'Layer 1', Frame 1, Line 7, Column 19 1195: Attempted access of inaccessible method LN10 through a reference with static type Class.//

NetSuite - Creating PO with line items

Using Restlets I can create any records in NetSuite. However, how do we create records with line items? I know we can use the getLineItemCount, loop through these items and use the setLineItemValue to set the line item.
What I'm not sure about is how we would pass such data to start with. So, we expect an external system to submit some data which I would then need to create POs with line items using my Restlet.
I would ideally like to test this using fire fox Poster, but not sure how to model the data. Something like this works fine to create a normal record using poster by passing data like:
{ "subsidiary" : 2, "entity" : 1084,"currency" : 2,"approvalstatus" : 2}
But how would we send line item data?
My JSon Object looks like this:
{"subsidiary" : 2,
"entity" : 1275,
"currency" : 2,
"approvalstatus" : 2,
"item": [{"item" : -3, "taxrate": 6},
{"item" : -3, "taxrate": 6}]
}
I tried getting the data out of the nested jason object with the below code but doesn't quite work...the itemid is blank
for (var x = 1; x <= jsonobject.item.length; x++)
{
var itemid = record.getLineItemValue('item', jsonobject.item['item'], x);
nlapiLogExecution('DEBUG', 'itemid', itemid)
record.setLineItemValue('item', itemid, x);
}
You could try using an array within your JSON to encapsulate the line items such as:
{"subsidiary" : 2,
"entity" : 1084,
"currency" : 2,
"approvalstatus" : 2,
"items": [{name:"item1", price: "100"},
{name:"item2", price:"200"}]
}
Your RESTlet code would have to then digest this, and call the relevant NS functions you mention.
As TonyH mentioned, your code has a bug wherein you should be getting the array index first. In addition, your index should start at 0, not 1, since you are going through a JS array, not a NetSuite sublist:
for (var x = 0; x < jsonobject.item.length; x++)
{
var itemid = jsonobject.item[x]['item'];
}
The same would go if you want to get the tax rate:
for (var x = 0; x < jsonobject.item.length; x++)
{
var taxrate = jsonobject.item[x]['taxrate'];
}

How to get only unique values of a 2d array

I need to get the following 2d array from:
[[Option 10, 2.0], [Option 10, 2.0], [Option 9, 1.0], [Option 7, 1.0]]
to
[[Option 10, 2.0], [Option 9, 1.0], [Option 7, 1.0]]
I found this post (Splitting a 2D array using some() , by date, avoiding duplicates. Just want 1 unique email, not row. Where am i wrong here?) that has a very efficient way of getting unique values, but I cannot figure out how to apply it to my situation.
Your use case is simpler than the one you refer to.
try this for example :
function myFunction() {
var source = [['Option 10', 2], ['Option 10', 2], ['Option 9', 1], ['Option 7', 1]];
var dest = [];
dest.push(source[0]);
for(var n = 1 ; n< source.length ; n++){
if(dest.join().indexOf(source[n].join()) == -1){dest.push(source[n])};
}
Logger.log(dest);
}
Because 'unique' is not always simple to describe, I often use a pattern which is is, in effect, a variation of Serge's correct answer using ES5 array map/filter functions.
An edited version:
function hash(arr) {
// in this case the hash method is the same as Serge's Array.join() method,
but could be customised to suit whatever condition you need to generate
bespoke comparators such as where `1 + 3` should match `2 + 2`, or where
particular columns in the array can be omitted
return arr.join();
}
function myFunction() {
var source = [['Option 10', 2], ['Option 10', 2], ['Option 9', 1], ['Option 7', 1]];
var hash = source.map(
function (row) {
return hash(row);
}
);
source = source.filter(
function (filterRow, i) {
return hash.slice(0, i).indexOf(hash(filterRow)) < 0;
}
);
Logger.log(source);
}
I only include this as there are times when your comparison may need to flex a little. In your example this isn't important which is why Serge's is correct, but I share to show a potential expansion food for thought for when unique needs to be 'tweaked'

Select from createListBox with ScriptDb

I have this code in input:
utentiGrid.setWidget(1, 2, app.createLabel('Stato:'));
utentiGrid.setWidget(1, 3,
app.createListBox().setName('tipologia').addItem("Alpha").addItem("Beta"));
I select "Beta" and write ScriptDb
Then I take the record and visualize the result by modifying with this code:
utentiGrid.setWidget(1, 2, app.createLabel('Stato:'));
utentiGrid.setWidget(1, 3,
app.createListBox().setName('tipologia').addItem("Alpha").addItem("Beta"));
The problem
I want see my first selection ("Beta") and not "Alpha".
how can I do?
thank you
raffaele
p.s. are not a programmer but scriptDb is fantastic!
Just use the method setSelectedItem as bellow:
function doGet() {
var app = UiApp.createApplication();
var utentiGrid = app.createGrid(3, 4);
utentiGrid.setWidget(1, 2, app.createLabel('Stato:'));
var listBox = app.createListBox().setName('tipologia').addItem("Alpha").addItem("Beta");
//To select Beta use 1 as argument
listBox.setItemSelected(1, true);
utentiGrid.setWidget(1, 3, listBox);
app.add(utentiGrid);
return app;
}
If you change the argument to 0 the item selected will be alpha. If you have a third element, their index will be 2 and so one.