Last active
June 12, 2023 18:07
-
-
Save ChecksumFailed/22ca8d69b1797dd77432119da0d34c6e to your computer and use it in GitHub Desktop.
ServiceNow: Convert Excel attachment to an object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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