Using Excel Outputs

Snap code, combined with an .xlsx file you use as a template, can create an Excel output file. Here are some examples.

 

In your output rule, use the "Add output field" block to insert whatever characters you want into a specified cell location.


A better approach is to avoid hard-coding fundamental elements you'll refer over and over, such as the name of the sheet.  Create a text variable to store the sheet name, so you can change it easily if you need to:


Usually you're not writing to an individual cell, but many cells (as a row), or many rows (as a table).  Here's how.  The following code starts on whatever sheet is set in the "tableSheetName" variable, and at whatever row is specified in the "tableSheetStartRow" variable. From there, it creates a table starting at column H, ending at column K.


You can also delete the contents of a cell.  For example, if a cell is filled with some text in the template, and you don't want it to appear, simply delete it.

 

Adding an image to an Excel sheet is done by specifying the image, and then defining where it will appear in a sheet.  This location is defined by the upper-right and lower-left cells to be covered by the image.

 

Some excel templates may have formulae defined in the template.  You can call this block which will try to update all those formulae, graphs, and other calculations.


The examples above are available for you to cut-and-paste into your own output rule.  Just select all the example code below(double- or triple-click), copy it, open up an output rule, right-click in the empty white workspace of the rule, choose "paste JSON", and press CTRL-V to paste it.

 

Example Code

[{"$type":"CommentBlock","disabled":true,"x":55,"y":139,"parts":{"comment":{"value":"Only generate this file if a certain condition is met"},"next":{"$type":"SetOutputPropertyBlock","disabled":true,"parts":{"property":{"value":"enabled"},"value":{"$type":"CompareBlock","parts":{"left":{"$type":"GetFieldValueByNameBlock","parts":{"name":{"$type":"LiteralStringBlock","parts":{"text":{"value":"myLanguage"}}}}},"operator":{"value":"=="},"right":{"$type":"LiteralStringBlock","parts":{"text":{"value":"en"}}}}},"next":{"$type":"CommentBlock","parts":{"comment":{"value":"Set the filename.  Make it obvious when this is not a production document."},"next":{"$type":"SetOutputPropertyBlock","parts":{"property":{"value":"targetFilename"},"value":{"$type":"StringConcatBlock","parts":{"v":2,"parts":[{"$type":"GetVariableBlock","parts":{"variable1":{"value":"quote"},"variable2":{"value":"name"}}},{"$type":"LiteralStringBlock","parts":{"text":{"value":"-"}}},{"$type":"CastBlock","parts":{"value":{"$type":"GetVariableBlock","parts":{"variable1":{"value":"quoteProduct"},"variable2":{"value":"id"}}},"type":{"value":"text"}}},{"$type":"LiteralStringBlock","parts":{"text":{"value":"-Summary"}}}]}},"next":{"$type":"IfBlock","parts":{"v":2,"if":{"$type":"CompareBlock","parts":{"left":{"$type":"GetVariableBlock","parts":{"variable1":{"value":"environment"}}},"operator":{"value":"!="},"right":{"$type":"LiteralStringBlock","parts":{"text":{"value":"prod"}}}}},"then":{"$type":"SetOutputPropertyBlock","parts":{"property":{"value":"targetFilename"},"value":{"$type":"StringConcatBlock","parts":{"v":2,"parts":[{"$type":"CastBlock","parts":{"value":{"$type":"GetOutputPropertyBlock","parts":{"property":{"value":"targetFilename"}}},"type":{"value":"text"}}},{"$type":"LiteralStringBlock","parts":{"text":{"value":"-"}}},{"$type":"GetVariableBlock","parts":{"variable1":{"value":"environment"}}}]}}}},"next":{"$type":"CommentBlock","parts":{"comment":{"value":"initialize some variables"},"next":{"$type":"DeclareVariableBlock","parts":{"itemVar":{"value":"introSheetName"},"type":{"value":"text"},"default":{"$type":"LiteralStringBlock","parts":{"text":{"value":"Sheet1"}}},"next":{"$type":"DeclareVariableBlock","parts":{"itemVar":{"value":"tableSheetName"},"type":{"value":"text"},"default":{"$type":"LiteralStringBlock","parts":{"text":{"value":"Sheet2"}}},"next":{"$type":"DeclareVariableBlock","parts":{"itemVar":{"value":"tableSheetStartRow"},"type":{"value":"number"},"default":{"$type":"NumberBlock","parts":{"number":{"value":"5"}}},"next":{"$type":"CommentBlock","parts":{"comment":{"value":"Write one field: basic syntax"},"next":{"$type":"AddOutputFieldBlock","parts":{"$propertySettings":{"externalId":{"$type":"LiteralStringBlock","parts":{"text":{"value":"$C$5@Sheet1"}}},"value":{"$type":"LiteralStringBlock","parts":{"text":{"value":"Hello, World!"}}}},"type":"excelCell","v":"HelloWorld","next":{"$type":"CommentBlock","parts":{"comment":{"value":"Write one field: suggested syntax"},"next":{"$type":"AddOutputFieldBlock","parts":{"$propertySettings":{"externalId":{"$type":"StringConcatBlock","parts":{"v":2,"parts":[{"$type":"LiteralStringBlock","parts":{"text":{"value":"$C$5"}}},{"$type":"LiteralStringBlock","parts":{"text":{"value":"@"}}},{"$type":"GetVariableBlock","parts":{"variable1":{"value":"introSheetName"}}}]}},"value":{"$type":"GetFieldValueByNameBlock","parts":{"name":{"$type":"LiteralStringBlock","parts":{"text":{"value":"Fmaterial"}}}}}},"type":"excelCell","v":"Project Name","next":{"$type":"CommentBlock","parts":{"comment":{"value":"Write many fields with a loop"},"next":{"$type":"DeclareVariableBlock","parts":{"itemVar":{"value":"ThisRow"},"type":{"value":"number"},"default":{"$type":"GetVariableBlock","parts":{"variable1":{"value":"tableSheetStartRow"}}},"next":{"$type":"ForEachBlock","parts":{"itemVar":{"value":"item"},"arrayVar":{"$type":"GetVariableBlock","parts":{"variable1":{"value":"configurator"},"variable2":{"value":"priceObject"},"variable3":{"value":"items"}}},"statement":{"$type":"AddOutputFieldBlock","parts":{"$propertySettings":{"externalId":{"$type":"StringConcatBlock","parts":{"v":2,"parts":[{"$type":"LiteralStringBlock","parts":{"text":{"value":"$H"}}},{"$type":"LiteralStringBlock","parts":{"text":{"value":"$"}}},{"$type":"CastBlock","parts":{"value":{"$type":"GetVariableBlock","parts":{"variable1":{"value":"ThisRow"}}},"type":{"value":"text"}}},{"$type":"LiteralStringBlock","parts":{"text":{"value":"@"}}},{"$type":"GetVariableBlock","parts":{"variable1":{"value":"tableSheetName"}}}]}},"value":{"$type":"GetVariableBlock","parts":{"variable1":{"value":"item"},"variable2":{"value":"sku"}}}},"type":"excelCell","v":"SKU","next":{"$type":"AddOutputFieldBlock","parts":{"$propertySettings":{"externalId":{"$type":"StringConcatBlock","parts":{"v":2,"parts":[{"$type":"LiteralStringBlock","parts":{"text":{"value":"$I"}}},{"$type":"LiteralStringBlock","parts":{"text":{"value":"$"}}},{"$type":"CastBlock","parts":{"value":{"$type":"GetVariableBlock","parts":{"variable1":{"value":"ThisRow"}}},"type":{"value":"text"}}},{"$type":"LiteralStringBlock","parts":{"text":{"value":"@"}}},{"$type":"GetVariableBlock","parts":{"variable1":{"value":"tableSheetName"}}}]}},"value":{"$type":"GetVariableBlock","parts":{"variable1":{"value":"item"},"variable2":{"value":"description"}}}},"type":"excelCell","v":"Desc","next":{"$type":"AddOutputFieldBlock","parts":{"$propertySettings":{"externalId":{"$type":"StringConcatBlock","parts":{"v":2,"parts":[{"$type":"LiteralStringBlock","parts":{"text":{"value":"$J"}}},{"$type":"LiteralStringBlock","parts":{"text":{"value":"$"}}},{"$type":"CastBlock","parts":{"value":{"$type":"GetVariableBlock","parts":{"variable1":{"value":"ThisRow"}}},"type":{"value":"text"}}},{"$type":"LiteralStringBlock","parts":{"text":{"value":"@"}}},{"$type":"GetVariableBlock","parts":{"variable1":{"value":"tableSheetName"}}}]}},"value":{"$type":"GetVariableBlock","parts":{"variable1":{"value":"item"},"variable2":{"value":"qty"}}}},"type":"excelCell","v":"Qty","next":{"$type":"AddOutputFieldBlock","parts":{"$propertySettings":{"externalId":{"$type":"StringConcatBlock","parts":{"v":2,"parts":[{"$type":"LiteralStringBlock","parts":{"text":{"value":"$K"}}},{"$type":"LiteralStringBlock","parts":{"text":{"value":"$"}}},{"$type":"CastBlock","parts":{"value":{"$type":"GetVariableBlock","parts":{"variable1":{"value":"ThisRow"}}},"type":{"value":"text"}}},{"$type":"LiteralStringBlock","parts":{"text":{"value":"@"}}},{"$type":"GetVariableBlock","parts":{"variable1":{"value":"tableSheetName"}}}]}},"value":{"$type":"GetVariableBlock","parts":{"variable1":{"value":"item"},"variable2":{"value":"extNetPrice"}}}},"type":"excelCell","v":"Price","next":{"$type":"SetVariableBlock","parts":{"variable1":{"value":"ThisRow"},"value":{"$type":"MathOperationBlock","parts":{"v":2,"parts":[{"$type":"GetVariableBlock","parts":{"variable1":{"value":"ThisRow"}}},"+",{"$type":"NumberBlock","parts":{"number":{"value":"1"}}}]}}}}}}}}}}}},"next":{"$type":"CommentBlock","parts":{"comment":{"value":"Delete the contents of a cell "},"next":{"$type":"IfBlock","parts":{"v":2,"if":{"$type":"CompareBlock","parts":{"left":{"$type":"FormatDateBlock","parts":{"date":{"$type":"NowBlock"},"format":{"$type":"LiteralStringBlock","parts":{"text":{"value":"$EEEE"}}}}},"operator":{"value":"!="},"right":{"$type":"LiteralStringBlock","parts":{"text":{"value":"Friday"}}}}},"then":{"$type":"AddOutputFieldBlock","parts":{"$propertySettings":{"externalId":{"$type":"LiteralStringBlock","parts":{"text":{"value":"$C$7@Sheet1"}}},"deleted":{"$type":"TrueFalseBlock","parts":{"value":{"value":"true"}}}},"type":"excelCell","v":"HideFridayDealText"}},"next":{"$type":"CommentBlock","parts":{"comment":{"value":"Add an image to a field"},"next":{"$type":"CommentBlock","parts":{"comment":{"value":"Note: adjust the media block used by the filePath parameter: click it to select media from your org's resources folder."},"next":{"$type":"AddOutputFieldBlock","disabled":false,"parts":{"$propertySettings":{"name":{"$type":"LiteralStringBlock","parts":{"text":{"value":"LogoField"}}},"filePath":{"$type":"ImageMediaBlock","disabled":false,"parts":{"image":{"value":"rubik's_cube.png"}}},"source":{"$type":"LiteralStringBlock","parts":{"text":{"value":"Media"}}},"sheetName":{"$type":"GetVariableBlock","parts":{"variable1":{"value":"introSheetName"}}},"upperLeftCell":{"$type":"LiteralStringBlock","parts":{"text":{"value":"C16"}}},"lowerRightCell":{"$type":"LiteralStringBlock","parts":{"text":{"value":"E22"}}}},"type":"excelImage","v":"CompanyLogo","next":{"$type":"CommentBlock","parts":{"comment":{"value":"Try to update formulae in the template"},"next":{"$type":"AddOutputFieldBlock","disabled":false,"parts":{"$propertySettings":{"externalId":{"$type":"GetVariableBlock","parts":{"variable1":{"value":"introSheetName"}}},"tryCalculateFormula":{"$type":"TrueFalseBlock","parts":{"value":{"value":"true"}}}},"type":"excelSheet","v":"UpdateFirstSheet"}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}]

 

Here's an example Excel template file, which matches the Snap code above: Excel.Output.Template.Example.xlsx


Was this article helpful?