how to use jstraverse update - hash

I have this object.
var users = {
'jane':{'sessionId':'12345','userid':'jane','data':{x:100,y:100}},
'rahul':{'sessionId':'12345','userid':'rahul','data':{x:100,y:100}}
};
I want to update the jane key data by addin {'status':'login'}
by using the jstraverse how can i do this?
This what i have tried ?
var Traverse = require('traverse');
var users = {
'jane':{'sessionId':'12345','userid':'jane','data':{x:100,y:100}},
'rahul':{'sessionId':'12345','userid':'rahul','data':{x:100,y:100}}
};
var scrubbed = Traverse(users).map(function (x) {
console.log(this.key);
this.update({'status':'login'});
});
but by which function i will get the key of jane and accoring to that i update here ?

Like this i have done is it ok?
var Traverse = require('hashish');
var users = {
'jane':{'sessionId':'12345','userid':'jane','data':{x:100,y:100}},
'rahul':{'sessionId':'12345','userid':'rahul','data':{x:100,y:100}}
};
console.log(users.jane);
var scrubbed = Traverse(users.jane)
.update({'status':'login'})
.items;
console.log(scrubbed);
users.jane=scrubbed;
console.log(users);

Related

I'm having problem binding data to content control using ooxml and office.js

I'm developing a word addin that pulls xml data from our servers and binds it to a rich text content control. The user should only be able to format the data within the content control but can't edit or delete the data.
This is the code i've been able to come up with but it's not working..
var ooxmlPackage = new DOMParser().parseFromString(
'<pkg:package xmlns:pkg="http://schemas.microsoft.com/office/2006/xmlPackage"></pkg:package>',
"text/xml"
);
var ooxmlPart = ooxmlPackage.createElement("pkg:part");
ooxmlPart.setAttribute("pkg:name", "/_rels/.rels");
ooxmlPart.setAttribute("pkg:contentType", "application/vnd.openxmlformats-package.relationships+xml");
ooxmlPart.setAttribute("pkg:padding", "512");
var ooxmlxmlData = ooxmlPackage.createElement("pkg:xmlData");
var ooxmlRelationships = ooxmlPackage.createElement("Relationships");
ooxmlRelationships.setAttribute("xmlns", "http://schemas.openxmlformats.org/package/2006/relationships");
var ooxmlRelationship = ooxmlPackage.createElement("Relationship");
ooxmlRelationship.setAttribute("Id", "rId1"); //TODO: generate unique ID
ooxmlRelationship.setAttribute(
"Type",
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument"
);
ooxmlRelationship.setAttribute("Target", "word/document.xml");
ooxmlRelationships.appendChild(ooxmlRelationship);
ooxmlxmlData.appendChild(ooxmlRelationships);
ooxmlPart.appendChild(ooxmlxmlData);
var ooxmlDocPart = ooxmlPackage.createElement("pkg:part");
ooxmlDocPart.setAttribute("pkg:name", "/word/document.xml");
ooxmlDocPart.setAttribute(
"pkg:contentType",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"
);
var ooxmlDocPartxmlData = ooxmlPackage.createElement("pkg:xmlData");
var ooxmlDocument = ooxmlPackage.createElement("w:document");
ooxmlDocument.setAttribute("xmlns:w", "http://schemas.openxmlformats.org/wordprocessingml/2006/main");
ooxmlDocument.setAttribute("xmlns:w15", "http://schemas.microsoft.com/office/word/2012/wordml");
var ooxmlDocumentBody = ooxmlPackage.createElement("w:body");
var ooxmlDocumentParagraph1 = ooxmlPackage.createElement("w:p");
var ooxmlContentControl = ooxmlPackage.createElement("w:sdt");
var sdtPr = ooxmlPackage.createElement("w:sdtPr");
//var lk = ooxmlPackage.createElement("w:lock");
//lk.setAttribute("w:val", "sdtContentLocked");
//sdtPr.appendChild(lk);
var alias = ooxmlPackage.createElement("w:alias");
alias.setAttribute("w:val", "MyControl");
sdtPr.appendChild(alias);
var id1 = ooxmlPackage.createElement("w:id");
id1.setAttribute("w:val", "1382295294");
sdtPr.appendChild(id1);
//var appr = ooxmlPackage.createElement("w15:appearance");
//appr.setAttribute("w15:val", "hidden");
//sdtPr.appendChild(appr);
sdtPr.appendChild(ooxmlPackage.createElement("w:showingPlcHdr"));
//var plchdlr = ooxmlPackage.createElement("w:placeholder");
//var docPrt = ooxmlPackage.createElement("docPart");
//docPrt.setAttribute("w:val", "defaultPlaceHolder_-1854013440");
//plchdlr.appendChild(docPrt);
//sdtPr.appendChild(plchdlr);
var bindingNode = ooxmlPackage.createElement("w15:dataBinding");
//bindingNode.setAttribute("w:prefixMappings","xmlns:ns0='custom'");
bindingNode.setAttribute("w:xpath","/Root[1]/Node[1]");
bindingNode.setAttribute("w:storeItemID", "{C2F77B86-6131-4922-803B-54FACB654C16}");
sdtPr.appendChild(bindingNode);
ooxmlContentControl.appendChild(sdtPr);
var sdtContent = ooxmlPackage.createElement("w:sdtContent");
var parag = ooxmlPackage.createElement("w:p");
var r = ooxmlPackage.createElement("w:r")
var t = ooxmlPackage.createElement("w:t")
//t.innerHTML= '[This is a test]';
r.appendChild(t);
parag.appendChild(r);
sdtContent.appendChild(parag);
ooxmlContentControl.appendChild(sdtContent);
ooxmlDocumentBody.appendChild(ooxmlDocumentParagraph1);
ooxmlDocumentBody.appendChild(ooxmlContentControl);
ooxmlDocument.appendChild(ooxmlDocumentBody);
ooxmlDocPartxmlData.appendChild(ooxmlDocument);
ooxmlDocPart.appendChild(ooxmlDocPartxmlData);
ooxmlPackage.documentElement.appendChild(ooxmlPart);
ooxmlPackage.documentElement.appendChild(ooxmlDocPart);
var serializer = new XMLSerializer();
var ooxmlPackageString = serializer.serializeToString(ooxmlPackage);
console.log(ooxmlPackageString);
Office.context.document.setSelectedDataAsync(ooxmlPackageString, { coercionType: "ooxml" });
Office.context.document.customXmlParts.getByIdAsync(
'<Root xmlns=""> <Node>VALUE1</Node></root>',
function (result) { });
It just inserts an empty content control when it runs, it doesn't map the xml node data as expected.

Populating RITM Variables from a REST Call

I'm in the process of learning scripted REST API's. I have one that creates a REQ and RITM when the user does a POST. I'm having trouble populating variables from the rest post to the RITM. Below is my current code. Response is the RITM, Requester, and short description. I'm open to all suggestions, like I said I'm learning this on the fly, so if there is any glaring problems, I'm all ears. Thanks!
(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
var cartId = GlideGuid.generate(null);
var cart = new Cart(cartId);
var item = cart.addItem('25409830135aa20099fc7e776144b0a0');
var rc = cart.placeOrder();
var req = request.getHeader('ContactEmployeeId');
var sdesc = request.getHeader('ShortDescription');
//return rc.number;
//gs.addInfoMessage(rc.number);
var num = rc.sys_id; //get sys ID of newly created request
var rnum = 0;
//using the request number, query the RITM table for related RITM
var u = new GlideRecord('sc_req_item');
u.addEncodedQuery('request='+num);
u.query();
while ( u.next() )
{
rnum = u.number;
}
response.setBody({
Requested_Item: rnum,
u_requester: req,
short_description: sdesc
});
response.setContentType('application/json');
})(request, response);
You can use the setVariable(ritm, variableName, value) method such as this.
var cartId = GlideGuid.generate(null);
var cart = new Cart(cartId);
var item = cart.addItem('25409830135aa20099fc7e776144b0a0');
// add your variables here to the item variable in your cart object
cart.setVariable(item, "comments", message);
cart.setVariable(item, "my_var_1", value1);
cart.setVariable(item, "my_var_2", value2);
// submit order
var rc = cart.placeOrder();

global mongodb cursor in Meteor App

I'm trying to build an app with Meteor and I'm new to the framework.
What I want to do is have the Mongocursor that get returned in the tablets functions with the search parameter to be used in colors functions. Currently in colors I'm just getting the reactive variable again and running the Tablets.find(). But I'm going to have many other functions like this so I want to use the same Mongo cursor. Is it possible to create a global Mongo cursor from the tablets functions that will also be reactive.
The code below is my setup:
Template.tabletsList.helpers({
tablets: function() {
// read the user's last search (if any)
var query = Template.instance().query.get();
// sort options
var options = {sort: {manufacturer: 1}};
if ($.isEmptyObject(query)) {
// if the user didn't input a search just find all tablets
var tabletCursor = Tablets.find({}, options);
return tabletCursor;
}
else {
// find all tablets matching the search expression
return Tablets.find(query, options);
}
},
colors: function() {
var query = Template.instance().query.get();
if ($.isEmptyObject(query))
var allTablets = Tablets.find().fetch();
else
var allTablets = Tablets.find(query).fetch();
var color = '';
var colors = [];
allTablets.forEach(function (tablet) {
for (var i=0; i<tablet.specs.color.length; i++) {
color = tablet.specs.color[i];
if ($.inArray(color, colors) === -1) {
colors.push(color);
}
}
});
return colors.sort();
}
You can create a global helper:
Template.registerHelper('tablets',function(query){
var options = {sort: {manufacturer: 1}};
if ($.isEmptyObject(query)) query = {};
return = Tablets.find(query, options);
});
Then from anywhere in your code you can access this with:
var tabletCursor = UI._globalHelpers.tablets(query);
If you don't have access to query everwhere you can add it to Session or create a new reactive variable to hold it.

Sending email from spreadsheet onOpen

I have a script that performs a CRM system on my company and I am using Fusion table as a database. Every time i enter a new record, this record is saved on this fusion table. When the save action is performed, I use the follow code to make a copy of the data, that is passed to the database, in on spreadsheet so I have a backup of the data:
function AtualizarSpreadsheet(e)
{
var app = UiApp.getActiveApplication();
var planilha = SpreadsheetApp.openById(SPREADSHEET_ID);
var sheet = planilha.getActiveSheet();
var enviado = "NÃO";
var id = e.parameter.id;
var nomeDoReclamante = e.parameter.nomeDoReclamante;
var telefone = e.parameter.telefone;
var ddd = e.parameter.ddd;
var prefixo = e.parameter.prefixo;
var sufixo = e.parameter.sufixo;
var email = e.parameter.email;
var regiao = e.parameter.regio;
var categoria = e.parameter.categoria;
var produto = e.parameter.produto;
var subcategoria = e.parameter.subcategoria;
var versaoDeSoftware = e.parameter.versoDeSoftware;
var dataDeInicio = e.parameter.dataDeInicio;
var dia = e.parameter.dia;
var mes = e.parameter.ms;
var ano = e.parameter.ano;
var tituloDoProblema = e.parameter.ttuloDoProblema;
var descricao = e.parameter.descrio;
var status = e.parameter.status;
var prioridade = e.parameter.prioridade;
var informarEngenharia = e.parameter.informarEngenharia;
var dataDeRegistro = e.parameter.dataDeRegistro;
var horaDeRegistro = e.parameter.horaDeRegistro;
var historico = e.parameter.histrico;
var ordem = e.parameter.ordem;
sheet.s
sheet.appendRow([enviado,
id,
nomeDoReclamante,
telefone,
ddd,
prefixo,
sufixo,
email,
regiao,
categoria,
produto,
subcategoria,
versaoDeSoftware,
dataDeInicio,
dia,
mes,
ano,
tituloDoProblema,
descricao,
status,
prioridade,
informarEngenharia,
dataDeRegistro,
horaDeRegistro,
historico,
ordem]);
return app;
}
On the spreadsheet I perform a function onOpen() to send one email everytime the spreadsheet is oppened.
However when my fusion table script performs the command: SpreadsheetApp.openById(SPREADSHEET_ID); the onOpen() function is not being triggered. Even if I oppen the spreadsheet I cant have that function runned.
The onOpen() function is detailed bellow:
function onOpen()
{
MailApp.sendEmail("EMAIL", "SUBJECT", "BODY");
}
What am I doing wrong?
You are attempting to call a service (MailApp) that is not supported in a simple trigger. You would need to call your onOpen() function something different, and attach an installable "on open" trigger to it instead.
https://developers.google.com/apps-script/understanding_triggers

Get Child name (relation) out of Contacts App

I'm extracting contacts with ContactsApp and can get email and namnes, but can not find out how to get "Child"-namne out of the contacts.
I tried with "CustomField". I can create a custom field and read it, but there is no "Child"-field.
How do I get "Child"-namne out of the contacts?
Test code for Google Apps script
function doGet(e) {
var GroupObject = ContactsApp.getContactGroup('TestGrupp');
var ContactsTestGrupp = GroupObject.getContacts();
var nrContacts = ContactsTestGrupp.length;
var objectContact;
var emailString;
var GivenNameString;
var FamilyNameString;
for(var i = 0; i < nrContacts; i++) {
objectContact = ContactsTestGrupp[i].getEmails();
emailString = objectContact[0].getAddress();
Logger.log(emailString);
GivenNameString = ContactsTestGrupp[i].getGivenName();
FamilyNameString = ContactsTestGrupp[i].getFamilyName();
Logger.log(GivenNameString + ' ' + FamilyNameString);
}
}
I don't think Google Contacts has a field for child name, at least not that I can find.