Skip to content

Instantly share code, notes, and snippets.

@theGove
Last active February 21, 2022 15:02
Show Gist options
  • Save theGove/ca0159f2543a9b2b4271d280fa7c0aa1 to your computer and use it in GitHub Desktop.
Save theGove/ca0159f2543a9b2b4271d280fa7c0aa1 to your computer and use it in GitHub Desktop.
A JADE module to download a pdf of the currently selected range
function auto_exec(){
Jade.open_automations(true, "Automations", [
{action:"Excel.run(make_pdf)",
name:"Make a PDF",
description:"Builds and downloads a PDF file"}
])
Jade.add_library('https://cdn.jsdelivr.net/npm/pdfmake@latest/build/pdfmake.min.js')
Jade.add_library('https://cdn.jsdelivr.net/npm/pdfmake@latest/build/vfs_fonts.min.js')
Jade.add_library('https://cdn.jsdelivr.net/npm/html-to-pdfmake/browser.js')
}
async function make_pdf(){
const html = await range_to_html_table()
const val = htmlToPdfmake(html);
createPdf({content:val}).download();
}
async function range_to_html_table(){
return await Excel.run(async function(excel){
const range = excel.workbook.getSelectedRange()
range.load("values")
await excel.sync();
const table=['<table style="border-collapse: collapse;border: 1px solid black;">']
for(const row of range.values){
table.push("<tr>")
for(const col of row){
table.push("<td>")
table.push(col)
table.push("</td>")
}
table.push("</tr>")
}
table.push("</table>")
return table.join("")
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment