From 771810fe5d3ff279b0bf786df419daef0bf7680d Mon Sep 17 00:00:00 2001 From: Alison McKay Date: Thu, 10 Jul 2025 16:26:04 -0700 Subject: [PATCH] [excel] (Samples) Add samples for save as PDF and send (#789) * [excel] (Samples) Add samples for save as PDF and send * Add save as PDF content * Add combine data sample * Add troubleshooting section * Add MailProperties tip * Fix link * Add preview tag * Fix link * Move tip * Remove unnecessary sample, adjust other articles to to align * Make PDF object more obvious * Update docs/resources/samples/save-and-email-as-pdf.md Co-authored-by: Alex Jerabek <38896772+AlexJerabek@users.noreply.github.com> * Explain download option per review feedback * Change filename to placate build validation --------- Co-authored-by: Alex Jerabek <38896772+AlexJerabek@users.noreply.github.com> --- ...combine-worksheets-into-single-workbook.md | 2 +- .../samples/save-as-pdf-email-as-pdf.md | 55 +++++++++++++++++++ docs/toc.yml | 2 + 3 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 docs/resources/samples/save-as-pdf-email-as-pdf.md diff --git a/docs/resources/samples/combine-worksheets-into-single-workbook.md b/docs/resources/samples/combine-worksheets-into-single-workbook.md index 6f39b89a..daac7fbe 100644 --- a/docs/resources/samples/combine-worksheets-into-single-workbook.md +++ b/docs/resources/samples/combine-worksheets-into-single-workbook.md @@ -1,6 +1,6 @@ --- title: Combine workbooks into a single workbook -description: Learn how to use Office Scripts and Power Automate to create merge worksheets from other workbooks into a single workbook. +description: Learn how to use Office Scripts and Power Automate to merge worksheets from other workbooks into a single workbook. ms.date: 11/29/2023 ms.localizationpriority: medium --- diff --git a/docs/resources/samples/save-as-pdf-email-as-pdf.md b/docs/resources/samples/save-as-pdf-email-as-pdf.md new file mode 100644 index 00000000..25e4dd3d --- /dev/null +++ b/docs/resources/samples/save-as-pdf-email-as-pdf.md @@ -0,0 +1,55 @@ +--- +title: Save and email as a PDF +description: Use Office Scripts to save a worksheet as a PDF and then email that PDF. +ms.date: 07/03/2025 +ms.localizationpriority: medium +--- + +# Save a worksheet and email it as a PDF (preview) + +Use Office Scripts to save a worksheet as a PDF and email it to yourself or your team. + +> [!NOTE] +> The script in this sample is provided as a preview for developers and may change based on feedback that we receive. Do not use this script in a production environment. + +## Solution + +1. Create a new Excel file in your OneDrive. +1. Add data to your workbook. +1. Create the script from this sample. +1. Replace `name@email.com` in this sample with your desired recipient email address. +1. Adjust the `subject` and `content` values. +1. Run the script. + +## Sample code: Save as a PDF and send via email + +```TypeScript +/** + * This script saves a worksheet as a PDF, downloads that PDF to your computer, and emails the PDF to a recipient. + */ +function main(workbook: ExcelScript.Workbook) { + // Create the PDF. + const pdfObject = OfficeScript.convertToPdf(); + const pdfFile = { name: "report.pdf", content: pdfObject }; // Enter your desired PDF name here. + + // Download the PDF. + OfficeScript.downloadFile(pdfFile); // Not required. Remove this line if you don't want to download the PDF. + + // Email the PDF. + OfficeScript.sendMail({ + to: "name@email.com", // Enter your recipient email address here. + subject: "[Demo] Monthly Sales Report", // This is the subject of your email. + content: "Here's the Monthly Sales Report", // This is the content within your email. + attachments: [pdfFile] + })    +} +``` + +> [!TIP] +> Use the properties of the [MailProperties](/javascript/api/office-scripts/excelscript/global.officescript.mailproperties) interface to add more details to your email, such as `cc`, `bcc`, and `importance` values. + +## Troubleshooting + +### Error: Protected document + +The [sensitivity label](https://support.microsoft.com/office/2f96e7cd-d5a4-403b-8bd7-4cc636bae0f9) for your workbook is preventing the script from sending an email. To resolve this error, change the sensitivity label of your workbook to General, Public, or Non-Business. Reload the workbook, and then run the script again. diff --git a/docs/toc.yml b/docs/toc.yml index e9198c1f..a689d583 100644 --- a/docs/toc.yml +++ b/docs/toc.yml @@ -80,6 +80,8 @@ items: href: resources/samples/excel-calculation.md - name: Move selected rows across tables href: resources/samples/move-rows-across-tables.md + - name: Save and email as a PDF + href: resources/samples/save-as-pdf-email-as-pdf.md - name: Notify people with comments href: resources/samples/add-excel-comments.md - name: Output table data as JSON