Skip to content

Instantly share code, notes, and snippets.

@the-paulus
Created April 15, 2017 21:05
Show Gist options
  • Save the-paulus/8f8707847e32ad7d0b6bc0b640113f5a to your computer and use it in GitHub Desktop.
Save the-paulus/8f8707847e32ad7d0b6bc0b640113f5a to your computer and use it in GitHub Desktop.
Google Sheet script that will convert cells A2:N1000 into a downloadable XML file.
/* Main function for generating XML from spreadsheet. */
function doGet() {
// The sheet that contains the product information.
var productDataSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Products");
// The first row of the sheet that contains all the product attributes (e.g., Color, Weight, Collection, Name, etc.)
var productAttributes = productDataSheet.getRange("A1:N1").getValues();
// Range of rows and columns that contain the product information.
var productData = productDataSheet.getRange("A2:N1000").getValues();
// XML document object.
var document = XmlService.createDocument();
// Root element of the XML output.
var root = XmlService.createElement('products');
// variable that holds the generated XML document.
var xmlOutput;
for(var i = 0; i < productData.length; i++) {
if(!isEnd(productData[i])) {
// Begin product tag.
var product = XmlService.createElement('product');
// Start iterating through each of the product's attributes.
for(var j = 0; j < productData[i].length; j++) {
var tag = productAttributes[0][j];
var text = productData[i][j];
if(tag == "categories") {
product.addContent(createCategories(text));
} else if( tag == "images") {
product.addContent(createImages(text));
} else if(tag == "collections_styles") {
product.addContent(createCollectionStyles(text));
} else {
var productAttribute = XmlService.createElement(tag)
if(tag == "description") {
productAttribute.addContent(XmlService.createCdata(text));
} else {
productAttribute.setText(text);
}
product.addContent(productAttribute)
}
}
root.addContent(product);
} else {
break;
}
}
// Put everything together and output it.
document.addContent(root);
xmlOutput = XmlService.getPrettyFormat().format(document);
return ContentService.createTextOutput(xmlOutput).downloadAsFile("products.xml");
}
/* Helper function for creating child tags for each category. */
function createCategories(categories) {
var elCategories = XmlService.createElement("categories");
// Clean up category name.
var categories = categories.replace(/_/g, " ").replace(/:/g, ",").split(',');
for(var i = 0; i < categories.length; i++) {
var elCategory = XmlService.createElement("category");
elCategory.setText(categories[i]);
elCategories.addContent(elCategory);
}
return elCategories;
}
/* Helper function for creating child tags for images. */
function createImages(images) {
var elImages = XmlService.createElement("images");
var images = images.split(",");
for(var i = 0; i < images.length; i++) {
var elImage = XmlService.createElement("image");
elImage.setText(images[i]);
elImages.addContent(elImage);
}
return elImages;
}
/* Helper function for creating child tags for collections and styles. */
function createCollectionStyles(collections_styles) {
var elCollectionStyles = XmlService.createElement("collections_styles");
var collections = collections_styles.split(",");
for(var i = 0; i < collections.length; i++) {
var elCollectionStyle = XmlService.createElement("collection_style");
elCollectionStyle.setText(collections[i]);
elCollectionStyles.addContent(elCollectionStyle);
}
return elCollectionStyles;
}
function isEnd(row) {
for(var i = 0; i < row.length; i++) {
if(row[i] != "") {
return false;
}
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment