armasmike
asked on
Need help with getting Dymo example code working with a json array
I am trying feed a json to a Dymo printer for labels. I found some old doc on it here
I want to use Option 2
Put I don't know how to import my json array in there code. On Line 30 is my Var mike it's getting the json array from a input box I used to store it. The json array is this
There are only 2 labels I know the Dymo being used only has 1 text field I will be making my own label and adding the xml to this file when it's done. I just need help in importing the json in to this so then I can use it's fields to feed the label.
I want to use Option 2
Put I don't know how to import my json array in there code. On Line 30 is my Var mike it's getting the json array from a input box I used to store it. The json array is this
[{"Barcode ":"M125942-002","part ":"F06984-0027","DESCR":"BACC69CCC0027J\/BOEIN, REV R","MFG":"M125942-002","Cable_Length":"27.125 Inches","Test_A":"0.03","Test_B":"0.05"},{"Barcode ":"M125942-003","part ":"F06984-0027","DESCR":"BACC69CCC0027J\/BOEIN, REV R","MFG":"M125942-003","Cable_Length":"27.125 Inches","Test_A":"0.06","Test_B":"0.04"}]
There are only 2 labels I know the Dymo being used only has 1 text field I will be making my own label and adding the xml to this file when it's done. I just need help in importing the json in to this so then I can use it's fields to feed the label.
//----------------------------------------------------------------------------
//
// $Id: GoogleSpreadSheet.js 38773 2015-09-17 11:45:41Z nmikalko $
//
// Project -------------------------------------------------------------------
//
// DYMO Label Framework
//
// Content -------------------------------------------------------------------
//
// DYMO Label Framework JavaScript Library Samples:
// Print mulltiple labels using Google Spreadsheet as a data source
//
//----------------------------------------------------------------------------
//
// Copyright (c), 2010, Sanford, L.P. All Rights Reserved.
//
//----------------------------------------------------------------------------
(function () {
var label;
var labelSet;
function onload() {
var printButton = document.getElementById('printButton');
var printersSelect = document.getElementById('printersSelect');
var mike = document.getElementById('mike');
function createLabelSet(json) {
var labelSet = new dymo.label.framework.LabelSetBuilder();
for (var i = 0; i < json.feed.entry.length; ++i) {
var entry = json.feed.entry[i];
//This is them backing down the array this is not the fields I will be using
var name = entry.gsx$name.$t;
var street = entry.gsx$streetaddress.$t;
var city = entry.gsx$city.$t;
var state = entry.gsx$state.$t;
var zip = entry.gsx$zip.$t;
var address = name + '\n' + street + '\n' + city + ', ' + state + ' ' + zip;
var record = labelSet.addRecord();
record.setText("Address", address);
}
return labelSet;
}
function loadSpreadSheetDataCallback(json) {
labelSet = createLabelSet(json);
};
window._loadSpreadSheetDataCallback = loadSpreadSheetDataCallback;
function loadSpreadSheetData() {
removeOldJSONScriptNodes();
var script = document.createElement('script');
script.setAttribute('src', 'http://spreadsheets.google.com/feeds/list/tSaHQOgyWYZb6mUPGgrsOGg/1/public/values?alt=json-in-script&callback=window._loadSpreadSheetDataCallback');
script.setAttribute('id', 'printScript');
script.setAttribute('type', 'text/javascript');
document.documentElement.firstChild.appendChild(script);
};
function removeOldJSONScriptNodes() {
var jsonScript = document.getElementById('printScript');
if (jsonScript)
jsonScript.parentNode.removeChild(jsonScript);
};
// create address label xml
function getAddressLabelXml() {
var labelXml = '<?xml version="1.0" encoding="utf-8"?>\
<DieCutLabel Version="8.0" Units="twips">\
<PaperOrientation>Landscape</PaperOrientation>\
<Id>Address</Id>\
<PaperName>30252 Address</PaperName>\
<DrawCommands>\
<RoundRectangle X="0" Y="0" Width="1581" Height="5040" Rx="270" Ry="270" />\
</DrawCommands>\
<ObjectInfo>\
<AddressObject>\
<Name>Address</Name>\
<ForeColor Alpha="255" Red="0" Green="0" Blue="0" />\
<BackColor Alpha="0" Red="255" Green="255" Blue="255" />\
<LinkedObjectName></LinkedObjectName>\
<Rotation>Rotation0</Rotation>\
<IsMirrored>False</IsMirrored>\
<IsVariable>True</IsVariable>\
<HorizontalAlignment>Left</HorizontalAlignment>\
<VerticalAlignment>Middle</VerticalAlignment>\
<TextFitMode>ShrinkToFit</TextFitMode>\
<UseFullFontHeight>True</UseFullFontHeight>\
<Verticalized>False</Verticalized>\
<StyledText>\
<Element>\
<String>DYMO\
828 San Pablo Ave Ste 101\
Albany, CA 94706-1678</String>\
<Attributes>\
<Font Family="Arial" Size="12" Bold="False" Italic="False" Underline="False" Strikeout="False" />\
<ForeColor Alpha="255" Red="0" Green="0" Blue="0" />\
</Attributes>\
</Element>\
</StyledText>\
<ShowBarcodeFor9DigitZipOnly>False</ShowBarcodeFor9DigitZipOnly>\
<BarcodePosition>AboveAddress</BarcodePosition>\
<LineFonts>\
<Font Family="Arial" Size="12" Bold="False" Italic="False" Underline="False" Strikeout="False" />\
<Font Family="Arial" Size="12" Bold="False" Italic="False" Underline="False" Strikeout="False" />\
<Font Family="Arial" Size="12" Bold="False" Italic="False" Underline="False" Strikeout="False" />\
</LineFonts>\
</AddressObject>\
<Bounds X="332" Y="150" Width="4455" Height="1260" />\
</ObjectInfo>\
</DieCutLabel>';
return labelXml;
}
function loadLabel()
{
// use jQuery API to load label
//$.get("Address.label", function (labelXml) {
label = dymo.label.framework.openLabelXml(getAddressLabelXml());
// }, "text");
}
// loads all supported printers into a combo box
function loadPrinters()
{
var printers = dymo.label.framework.getLabelWriterPrinters();
if (printers.length == 0)
{
alert("No DYMO LabelWriter printers are installed. Install DYMO LabelWriter printers.");
return;
}
for (var i = 0; i < printers.length; ++i) {
var printer = printers[i];
var printerName = printer.name;
var option = document.createElement('option');
option.value = printerName;
option.appendChild(document.createTextNode(printerName));
printersSelect.appendChild(option);
}
}
// prints the label
printButton.onclick = function () {
try {
if (!label)
throw "Label is not loaded";
if (!labelSet)
throw "Label data is not loaded";
label.print(printersSelect.value, '', labelSet);
// var records = labelSet.getRecords();
// for (var i = 0; i < records.length; ++i)
// {
// label.setObjectText("Address", records[i]["Address"]);
// var pngData = label.render();
//
// var labelImage = document.getElementById('img' + (i + 1));
// labelImage.src = "data:image/png;base64," + pngData;
// }
}
catch (e) {
alert(e.message || e);
}
};
loadLabel();
loadSpreadSheetData();
loadPrinters();
};
function initTests()
{
if(dymo.label.framework.init)
{
//dymo.label.framework.trace = true;
dymo.label.framework.init(onload);
} else {
onload();
}
}
// register onload event
if (window.addEventListener)
window.addEventListener("load", initTests, false);
else if (window.attachEvent)
window.attachEvent("onload", initTests);
else
window.onload = initTests;
}());
Where and how do you invoke your code? What has Google Spreadsheets to do with it? Can you elaborate your project a little further?
ASKER
Wow I got it working
added this to the function onload()
added this to the function onload()
var mike = '[{"Barcode":"M125942-002","part":"F06984-0027"}]';
var obj = jQuery.parseJSON(mike);
and this function createLabelSet(json)
{
var labelSet = new dymo.label.framework.LabelSetBuilder();
for (var i in obj)
{
var itemDescription = obj[i].Barcode;
var itemCode = obj[i].part;
var record = labelSet.addRecord();
record.setText("Description", itemDescription);
record.setText("ItemCode", itemCode);
}
return labelSet;
}
And is worked so from here I can go crazy and add way more stuff.
This question needs an answer!
Become an EE member today
7 DAY FREE TRIALMembers can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.