JSON Data to Javascript Array Undefined - javascript

I am trying to get data from a JSON file and use Javascript Code to put it into Google Spreadsheet, I did good with some part of the data but I'm stuck with the other part, where I want to match the User Id and put all the data with it on a same row,
JSON Data:
"m":{"414":{"a":{"0":{"c":38,"p":12812.4},
"4":{"c":35,"p":10559.94},"2":{"c":43,"p":35811.63},
"6":{"c":48,"p":45530}},"d":{"0":{"c":55,"p":5477.06225},
"4":{"c":694,"p":106649.473},"2":{"c":1844,"p":716733.50775000011},
"6":{"c":605,"p":324152.5875}},"i":{"0":{"c":0,"p":0},
"4":{"c":0,"p":0},"2":{"c":0,"p":0},"6":{"c":542,"p":19893.93}}},
"404":{"a":{"0":{"c":15,"p":916.182},"4":{"c":50,"p":12357},
"2":{"c":530,"p":390825.27},"6":{"c":58,"p":4841.55}},
"d":{"0":{"c":10,"p":3145.8},"4":{"c":770,"p":141876.12},
"2":{"c":4854,"p":2173966.6125000003},
"6":{"c":1973,"p":1145077.425}},"i":{"0":{"c":0,"p":0},
"4":{"c":0,"p":0},"2":{"c":0,"p":0},"6":{"c":594,"p":25444.41}}}},
Javascript:
var testUF = [];
var Uid = Object.getOwnPropertyNames(doc1.m);
for (var lp2 = 0; lp2 < Uid.length; lp2++) {
var Ua1 = doc1.m[lp2].a["0"].p;
var Ua2 = doc1.m[lp2].a["4"].p;
var Ua3 = doc1.m[lp2].a["2"].p;
var Ua4 = doc1.m[lp2].a["6"].p;
var Ud1 = doc1.m[lp2].d["0"].p;
var Ud2 = doc1.m[lp2].d["4"].p;
var Ud3 = doc1.m[lp2].d["2"].p;
var Ud4 = doc1.m[lp2].d["6"].p;
var Ui4 = doc1.m[lp2].i["6"].p;
testUF.push([Uid,Ua1,Ua2,Ua3,Ua4,Ud1,Ud2,Ud3,Ud4,Ui4]);}
I am getting the Array on the Uid while Debugging, but all the other Variables don't get the data it stays Undefined. I want all the other variables to match with the Uid's and stay in the same row. I did the JSON parsing and everything.
I am asking for the first time on stackoverflow, please forgive me if I couldn't state everything properly. Thank you for the help. :)

You're not using the array index for Uid properly.
Change:
var Ua1 = doc1.m[lp2].a["0"].p;
To:
var Ua1 = doc1.m[Uid[lp2]].a["0"].p;

Related

Issue in creating script that copy the sheet and update it based on form response

I am trying to make a copy of Template in same folder when user submits the form and than update the fields in new copied sheet based on what user has submitted in the form response.
New sheet is creating successfully but response I am getting in the form is not updating in the new sheet.
Need help in finding the issue in code
error showing :
TypeError: Cannot read property '0' of undefined
at CopySheet(Code:32:63)
at getResponse(Code:3:16)
function getResponse(e){
var response = e.response;
var sheetID = CopySheet(response);
}
function CopySheet(response) {
var ss = SpreadsheetApp.openById('1Q5XRV7E-m6IleX4vG2tqmJ104Osz6rb67tc8SA_1avk');
var ssname = ss.getSheetByName('Template');
var file = DriveApp.getFileById(ss.getId());
var folders = file.getParents();
while (folders.hasNext())
{
var folder = folders.next();
var FID = folder.getId();
}
var protection = ssname.getProtections(SpreadsheetApp.ProtectionType.RANGE);
var editors = protection[0].getEditors();
Logger.log(editors);
var TargetFolder = DriveApp.getFolderById(FID);
var CSheetID = DriveApp.getFileById(ss.getId()).makeCopy("CopiedTemplate", TargetFolder).getId();
var CopiedSheet = SpreadsheetApp.openById(CSheetID);
var ptc = CopiedSheet.getProtections(SpreadsheetApp.ProtectionType.RANGE);
for (var j = 0 ; j<ptc.length ; j++)
{
ptc[j].addEditors(editors);
}
CopiedSheet.getRange('C1').setValue(response['S.C.O Number'][0]);
CopiedSheet.getRange('F1').setValue(response['Qty'][0]);
CopiedSheet.getRange('E2').setValue(response['PCBA Code'][0]);
DriveApp.getFileById(CSheetID).setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.EDIT);
return CSheetID;
}
Edited Code :
function getResponse(e){
var response2 = e.response;
var IResponse = response2.getItemResponses();
var SCO_Number = IResponse[0].getResponse();
var BOM_Code = IResponse[1].getResponse();
var qty = IResponse[2].getResponse();
var SID = CopySheet(SCO_Number,BOM_Code,qty);
}
That's because you are accessing the 0th index of a null variable.
Use e.values instead:
Code:
function getResponse(e){
var response = e.values;
Logger.log(response);
// var sheetID = CopySheet(response);
}
and upon checking the value of the response, it should look like this.
Index 0 of response is the date the form was submitted.
Index 1 is the answer of the 1st question
Index 2 is for the 2nd question, and so on and so forth
Just use the order of the question then use it as index.
Sample:
If value of S.C.O Number is the answer of the first question, then getting the value of the answer should be response[1]
Code:
CopiedSheet.getRange('C1').setValue(response[1]); // answer to 1st question
CopiedSheet.getRange('F1').setValue(response[2]); // answer to 2nd question
CopiedSheet.getRange('E2').setValue(response[3]); // answer to 3rd question

How to change JSON into string

i got a words array in JSON form like:
var words = [];
words.push({word:"I",x:50,y:50});
and i want it to store into a txt file which user can download from the website.
...
var data = JSON.stringify(words);
var file = new Blob([data],{type:'plain/text'});
...
however the output file is like this:
{"word":"Peaceful","x":40,"y":65,"lyric":"lyric"}
but i want it only print out the word "peaceful", i tried to use
var data = JSON.stringify(words.word);
but the output shows undefined.
words is an array, so you have to index it to access the object that you pushed onto it:
var data = JSON.stringify(words[0].word);
If I understand you correctly.
var myWords = [];
(for var i = 0; i < words.length; i++) {
var word = words[i].word;
myWords.push(word);
}
myWords.join(' '); // or myWords.join('\n');

How to get periodId on DashMetrics

In dash.js there is a function getBandwidthForRepresentation() on file DashMetrics.js. It needs two parameters: representationId and periodId. I can use getCurrentRepresentationSwitch() to get representationId. But i don't know how to get periodId? With which function can i get the data?
I tried to see example on dash-reference-client, it is confusing and still have no idea.
Thanks
I know your question is kind of old and you are probably not needing this anymore, but I'll send some code in case someone needs it eventually.
As you stated, getBandwidthForRepresentation() expects 2 parameters: representationId and periodId
How to get representationId:
you need to get metrics: var metrics = player.getMetricsFor('video')
and dashMetrics: var dashMetrics = player.getDashMetrics()
now we get representationId: var representationId = dashMetrics.getCurrentRepresentationSwitch(metrics).to
How to get peropdId:
we get the active stream info: var streamInfo = player.getActiveStream().getStreamInfo()
we use index as periodId: var periodId = streamInfo.index
How to get bandwidth:
we can now get bandwidth: var bandwidth = dashMetrics.getBandwidthForRepresentation(representationId, periodId)
Full Code:
var metrics = player.getMetricsFor('video')
var dashMetrics = player.getDashMetrics()
var representationId = dashMetrics.getCurrentRepresentationSwitch(metrics).to
var streamInfo = player.getActiveStream().getStreamInfo()
var periodId = streamInfo.index
var bandwidth = dashMetrics.getBandwidthForRepresentation(representationId, periodId)
Hope it helps

how to get child data of json Object

Ive got this JSON object Im trying loop through which is returned via a Netsuite script.
its returning a JSON object that has some child elements.
Im trying to get the caption for classnoheirachy - name data, but everything im just not sure how to get the inner most child.
Id like to know how in javascript I can output the classnoheirachy - name and classnoheiracy-internalId
My code is below for how im looping it at the moment, but if anyone knows how i can get the items above into this newly created array that would be greatly appreciated.
var rs = nlapiSearchRecord('item', 'customsearch_pm_item_record_loader', itemFilters);
response.write(JSON.stringify(rs));
var retObj = [];
for ( var i = 0; rs != null && i < rs.length; i++) {
var rowObj = {};
rowObj.LOCATION = rs[i].getValue('location');
rowObj.LOCATION_AVAILABLE = rs[i].getValue('locationquantityavailable');
rowObj.CLASS = rs[i].getValue('classnohierarchy');
rowObj.CURRENCY = rs[i].getValue('currency', 'pricing');
rowObj.UNIT_PRICE = rs[i].getValue('unitprice', 'pricing');
rowObj.LOCATION_ON_HAND = rs[i].getValue('locationquantityonhand');
rowObj.ITEM = rs[i].getValue('itemid');
rowObj.ITEM_NAME = rs[i].getValue('itemid');
rowObj.PRICE_LEVEL = rs[i].getValue('pricelevel', 'pricing');
rowObj.ITEM_DESCRIPTION = rs[i].getValue('salesdescription');
retObj.push(rowObj);
}
Try something like:
rowObj.Name = rs[i]['columns']['classnohierarchy']['name'];
rowObj.InternalId = rs[i]['columns']['classnohierarchy']['internalid'];
See Fiddle Here for an example

cannot get localStorage to work

Here is my fiddle: jsfiddle.net/XR8EZ
I cannot get the save data to load after clicking the load button. Can anyone help me here?
Sorry for the length =D
The load routine is wrong.
You save all your datas (stringifyed) with setItem('serializedObj').
Then you retrieve value with "retrievedItem = localStorage.getItem('serializedObj');" but not save the value of "JSON.parse(retrievedItem);".
Then you try to retrieve values directly by localStorage.getItem(imgName);
You retrive and save data parded:
dataparsed = JSON.parse(retrievedItem);
Then you must use this value for retrieving values:
//Reconstruct the original image array
for (i=0;i<totalState.length;i++) {
var thisParseImgVal = dataparsed.imgName;
totalImage[i] = JSON.parse(thisParseImgVal);
}
You have to correct you save/load code, I suggest you to save you data as array:
var imgVal = JSON.stringify(totalImage);
var shpVal = JSON.stringify(totalShape);
var hLval = JSON.stringify(totalHighlight);
var str = {imgName:imgVal,shpName:shpVal,hLname:hLval}; //Store
So you must read it as:
totalImage[i] = JSON.parse(thisParseImgVal[i]);

Categories