Adding color to Netsuite suitelet's sublist row depending upon the result - dom

Is there any way to add colors to a sublist's row depending upon a condition. I have loaded a saved search to show output on a sublist. But now I want to highlight the rows if the difference between todays date and audit date(search output) is more than 100 days.
var search = nlapiLoadSearch('customrecord_cseg_properties', 'customsearch52');
var columns=search.getColumns();
var sublist = form.addSubList('customsublist', 'staticlist', 'List of properties');
for(var i = 0; i< columns.length; i++){
sublist.addField('customcolumn'+i, 'text', columns[i].getLabel());
}
var result= search.runSearch();
var resultIndex = 0,resultStep = 1000,resultSet,resultSets = [];
do {
resultSet = result.getResults(resultIndex, resultIndex + resultStep);
resultSets = resultSets.concat(resultSet);
resultIndex = resultIndex + resultStep;
} while (resultSet.length > 0);
nlapiLogExecution('DEBUG','The Total number of rows is',resultSets.length);
for(var w= 0; w<resultSets.length ;w++){
for(var x=0; x<columns.length; x++){
var temp;
temp=resultSets[w].getText(columns[x]);
if(temp==null || temp==''){
temp=resultSets[w].getValue(columns[x]);
}
sublist.setLineItemValue('customcolumn'+x, Number(w)+1,temp);
}
I couldn't find any functions in UI Builder API for Netsuite for doing this. Please let me know if there is any other way to do this. Above is the code which I have used to display search result in suitelet.

There is no native api for that.
You can do it by mainpulating the DOM on the client script onInit function.
Just keep in mind that DOM manipulation are risky since they can break if NetSuite will chnage the DOM structure.

Related

Is it necessary to create <span> elements to register event listeners

I have a working web app that reads local .txt files and displays the content in a div element. I create a span element out of each word because I need to be able to select any word in the document and create an EEI (Essential Elements of Information) from the text. I then register a click handler on the containing div and let the event bubble up. The three functions below show reading the file, and parsing it, and populating the text div with spans:
function readInputFile(evt) {
reset();
var theFile = evt.target.files[0];
if(theFile) {
$("#theDoc").empty(); //Clean up any old docs loaded
var myReader = new FileReader();
var ta = document.getElementById("theDoc");
myReader.onload = function(e) {
parseTheDoc(e.target.result);
initialMarkup();
};
myReader.readAsText(theFile);
} else {
alert("Can not read input file: readInputFile()");
}
}
function parseTheDoc(docContents) {
var lines = docContents.split("\n");
var sentWords =[];
for(var i = 0; i < lines.length; i++) {
sentWords = lines[i].split(" ");
words = words.concat(sentWords);
words.push("<br>");
}
//examineWords(words);
createSpans(words);
}
function createSpans() {
for (var i = 0; i < words.length; i++) {
var currentWord = words[i];
if(currentWord !== "<br>") {
var $mySpan = $("<span />");
$mySpan.text(currentWord + " ");
$mySpan.attr("id", "word_" + i);
$("#theDoc").append($mySpan);
buildDocVector(currentWord, i, $mySpan);
}
else {
var $myBreak = $("<br>");
$myBreak.attr("id", "word_" + i);
$("#theDoc").append($myBreak);
buildDocVector("br", i, $myBreak);
}
}
//console.log("CreateSpans: Debug");
}
So basically a simple fileReader, split on \n, then tokenize on white space. I then create a span for each word, and a br element for each \n. It's not beautiful, but it satisfies the requirement, and works. My question is, is there a more efficient way of doing this? It just seems expensive to create all these spans, but my requirement is to annotate the doc and map any selected word to a data model/ontology. I can't think of a way to allow the user to select any word, or combination of words (control click) and then perform operations on them. This works, but with large docs (100 pages) I start having performance/memory issues. I understand this is more a design question and may not be appropriate, but I'd really like to know if there are more performant solutions.

List all Labels of an email to Spreadsheet

My emails usually has more than one Labels assigned. I like to search emails with a specific label then list them into the spreadsheet and show all other labels also assigned to the email.
Here's what i have so far, can't figure out how to get the other labels...
function myFunction() {
var ss = SpreadsheetApp.getActiveSheet();
var threads = GmailApp.search("label:Test");
for (var i=0; i<threads.length; i++)
{
var messages = threads[i].getMessages();
for (var j=0; j<messages.length; j++)
{
var sub = messages[j].getSubject();
var from = messages[j].getFrom();
var dat = messages[j].getDate();
ss.appendRow([dat, sub, from])
}
}
}
As far as Apps Script is concerned, Gmail labels are applied to threads and not to individual messages. (There are other contexts where this isn't necessarily true, as a Web Apps post details).
So, you should use the getLabels method of the Thread object. It then makes sense to structure the output so that each row corresponds to a thread, rather than a message. This is what I did below. The script takes subject/from/date from the first message in each thread. The 4th column is the comma-separated list of labels, except the one you search for.
function myFunction() {
var sheet = SpreadsheetApp.getActiveSheet();
var search_label = 'Test';
var threads = GmailApp.search('label:' + search_label);
var output = [];
for (var i=0; i < threads.length; i++) {
var firstMessage = threads[i].getMessages()[0];
var sub = firstMessage.getSubject();
var from = firstMessage.getFrom();
var dat = firstMessage.getDate();
var labels = threads[i].getLabels();
var otherLabels = [];
for (var j = 0; j < labels.length; j++) {
var labelName = labels[j].getName();
if (labelName != search_label) {
otherLabels.push(labelName);
}
}
output.push([dat, sub, from, otherLabels.join(', ')]);
}
sheet.getRange(1, 1, output.length, output[0].length).setValues(output);
}
I prefer not to add one row at a time, instead gathering the double array output and inserting it all at once. Of course you can use appendRow as in your script. Then you wouldn't necessarily need a comma-separated list,
sheet.appendRow([dat, sub, from].concat(otherLabels));
would work.

Find if given lat-long lies in any of the polygon in MongoDB

I want to know if I get a lat-long of a user and want to check if he lies in any of the polygon that is stored in my db (MongoDB). How can this be achieved using mongoDB.
For instance my db will have 10 polygons stored as GeoJson objects. I get a lat-long and want to check if this lat-long lies in any of the 10 polygons in my DB.
Can this be achieved in MongoDB?
I dont think your particular case can be achieved in MongoDB. MongoDB does have polygon calculations with the $polygon operator. However, this operator takes a polygon and confirms if any documents are within it not the other way round.
I wrote this code in javascript it is working fine for me
var polygonData = "43.64486433588385,-79.3791389465332;43.64508171979899,-79.3930435180664;43.63682057801007,-79.38437461853027;43.63946054004705,-79.36819553375244;43.652720712083266,-79.37201499938965;43.65793702655821,-79.39111232757568;43.64927396999741,-79.37222957611084";
var lat='43.64927396999741';
var lng='-79.37222957611084';
polySearch(polygonData,lat,lng);
function polySearch(polygonData,lat,lng){
var lat_long=lat+","+lng;
var longitude_x = lng;
var latitude_y = lat;
var is_in_polygon=false;
var vertices_x=[];
var vertices_y=[];
var arr=polygonData.split(';');
for(var j=0; j<arr.length; j++){
var arr1=arr[j].split(',');
vertices_x.push(arr1[1]);
vertices_y.push(arr1[0]);
}
var points_polygon = vertices_x.length;
var isIn=isInPolygon(points_polygon, vertices_x, vertices_y, longitude_x, latitude_y);
if (isIn==1)
console.log('Which is in polygon');
else
console.log('Which is not in polygon');
function isInPolygon(points_polygon, vertices_x, vertices_y, longitude_x, latitude_y)
{
var i = 0;
var j = 0;
var c = 0;
for (i = 0, j = points_polygon-1 ; i < points_polygon; j = i++) {
var val="((("+vertices_y[i]+" > "+latitude_y+") != ("+vertices_y[j]+" > "+latitude_y+")) && ("+longitude_x+" < ("+vertices_x[j]+" - "+vertices_x[i]+") * ("+latitude_y+" - "+vertices_y[i]+") / ("+vertices_y[j]+" - "+vertices_y[i]+") + "+vertices_x[i]+"))";
if(eval(val))c=!c;
}
return c;
}
}
Sorry to only past two links, but i think these explains what should be done far better then i would:
A SO answer in the same subject
An excellent tutorial how to use mongodb geospatial indexes

Template.templatename.rendered can't return any data from mongodb

I would like to know how can I get the data from mongodb in Template.templatename.rendered function. I tried click event on other template and it's all working fine and return the results i wanted. But what I need is to rendered the chart on load. But I could not get any data from the mongodb.
//poll.js
var drawPollChart = function(){
//returns data on other template methods except for
//Template.templatename.rendered
var dist = getDistinctQuestionId();
alert('dist:'+dist);
var data_x =[];
for(var i=0; i< 1; i++)
{
var count = getDataCount(dist[i]);
var uniq = getDistinctResponseBucket(dist[i]);
for(var j=0; j<uniq.length; j++)
{
//alert('data:' + count[uniq[j]] + ", label:" + uniq[j]);
data_x.push({
data : count[uniq[j]],
label: uniq[j]
});
}
}
Template.pollChart.rendered = function() {
//can't draw a thing cause can't get any data from mongodb
drawPollChart();
};
Help please? Thanks in advance.
Just because the template is rendered doesn't mean the DB is connected.
Use Meteor.status().status to detect the state of the connection. For example you could wait to render the pollChart-template at all until Meteor.status().status === 'connected'.

Google Spreadsheet Script problem - Error: Service Times Out: Apps Script

I've been trying to whip up a quick google script to count rsvps for the invite response spreadsheet for a wedding. The script worked perfectly for a week as new entries were added to the spreadsheet, then suddenly stopped working with the following error message in each cell:
Error: Service Times Out: Apps Script
The script itself is simple. It queries the relevant column (there are multiple events) and then checks to see whether there is some response spefied by the user - "YES", "NO", or a blank, typically.
What does this error mean, and does anyone have any suggestions for fixes?
function sumRSVP(response, rsvpType) {
var rsvpCol = 7;
if (rsvpType == "rehearsal") rsvpCol = 8;
if (rsvpType == "brunch") rsvpCol = 9;
var mySum = 0;
var sh = SpreadsheetApp.getActiveSheet();
for( i=2; i<177; i++){
var rsvp = sh.getRange(i, rsvpCol).getValue();
var nguests = sh.getRange(i, 6).getValue();
if(nguests != "" && rsvp == response){
mySum = mySum + parseFloat(nguests);
}
}
return mySum;
}
Hopefully the wedding went well. This was asked some time ago but has been viewed over 300 times at this post and I believe is important:
Data should not be extracted from a spreadsheet in a loop. The data needed should be extracted in a batch to an array and the array evaluated in the loop.
See docs reference at:
https://developers.google.com/apps-script/guide_common_tasks#OptimizeScripts
You can write scripts to take maximum advantage of the built-in caching, by minimizing the number of reads and writes. Alternating read and write commands is slow. To speed up a script, read all data into an array with one command, perform any operations on the data in the array, and write the data out with one command.
function sumRSVP(response, rsvpType) {
var rsvpCol = 7;
if (rsvpType == "rehearsal") rsvpCol = 8;
if (rsvpType == "brunch") rsvpCol = 9;
var mySum = 0;
var sh = SpreadsheetApp.getActiveSheet();
// start at row 2 - uses columns 6-9
var data = sh.getRange(2, 6, 177 - 1 , 4).getValues();
for(var i=0; i<data.length; i++){
var rsvp = data[i][rsvpCol - 6];
var nguests = data[i][0];
if(nguests != "" && rsvp == response){
mySum = mySum + parseFloat(nguests);
}
}
return mySum;
}