Skip to content

Instantly share code, notes, and snippets.

@ChecksumFailed
Last active June 12, 2023 18:07
Show Gist options
  • Save ChecksumFailed/22ca8d69b1797dd77432119da0d34c6e to your computer and use it in GitHub Desktop.
Save ChecksumFailed/22ca8d69b1797dd77432119da0d34c6e to your computer and use it in GitHub Desktop.
ServiceNow: Convert Excel attachment to an object
function excelToObject(attID) {
if (!attID || !isValidSysId(attID)) {
throw new Error("Valid attachment id required");
}
var excelObj = [];
var attachment = new GlideSysAttachment();
var attachmentStream = attachment.getContentStream(attID);
var parser = new sn_impex.GlideExcelParser();
parser.parse(attachmentStream);
while (parser.next()) {
excelObj.push(parser.getRow());
}
//return JSON.stringify(excelObj); stringify if a client callable script
return excelObj;
}
function isValidSysId(sysid) {
var pattern = /[a-zA-Z0-9]{32}/;
return pattern.test(sysid);
}
try {
excelToObject('SomeRandomAttachmentSysID');
}catch (ex){
gs.print('errrr: ' + ex.message);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment