Skip to content

[excel] (Samples) Add samples for save as PDF and send #789

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions docs/resources/samples/combine-data-from-multiple-worksheets.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
---
title: Combine data from multiple worksheets
description: Learn how to use Office Scripts to merge data from other worksheets into a single worksheet.
ms.date: 07/03/2025
ms.localizationpriority: medium
---

# Combine data from multiple worksheets

Use Office Scripts to combine data from multiple worksheets into a single worksheet, creating a unified report of your data.

> [!IMPORTANT]
> This sample only copies the values from the other worksheets. It does not preserve formatting, charts, tables, or other objects.

## Solution

1. Create a new Excel file in your OneDrive.
1. Add data to multiple worksheets.
1. Create the script from this sample.
1. Run the script.

## Sample code: Combine data

```TypeScript
/**
* This script returns the values from the used ranges on each worksheet, and then combines them on a new worksheet.
*/
function main(workbook: ExcelScript.Workbook) {
// Create an object to return the data from each worksheet.
let worksheetInformation: WorksheetData[] = [];

// Get the data from every worksheet, one at a time.
workbook.getWorksheets().forEach((sheet) => {
let values = sheet.getUsedRange()?.getValues();
worksheetInformation.push({
name: sheet.getName(),
data: values as string[][]
});
});

// Create the new worksheet.
let sheet = workbook.addWorksheet("Combined Data");

// Add data from each worksheet to new worksheet.
worksheetInformation.forEach((value) => {
// If there was any data in the worksheet, add it to a new range.
if (value.data) {
let range = sheet.getRangeByIndexes(0, 0, value.data.length, value.data[0].length);
range.insert(ExcelScript.InsertShiftDirection.down);
range.setValues(value.data);
}
});
}

// An interface to pass the worksheet name and cell values through a flow.
interface WorksheetData {
name: string;
data: string[][];
}
```

## Next steps

Learn how to [save your report as a PDF and email it](save-and-email-as-a-pdf.md) to yourself or your team.
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
---
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.
title: Combine worksheets with Power Automate
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
---

# Combine worksheets into a single workbook
# Combine worksheets into a single workbook with Power Automate

This sample shows how to pull data from multiple workbooks into a single, centralized workbook. It uses two scripts: one to retrieve information from a workbook and another to create new worksheets with that information. It combines the scripts in a Power Automate flow that acts on an entire OneDrive folder.

Expand Down
42 changes: 42 additions & 0 deletions docs/resources/samples/save-and-email-as-pdf.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
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

Use Office Scripts to save a worksheet as a PDF and email it to yourself or your team.

## 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. Run the script.

## Sample code: Save as a PDF and send via email

```TypeScript
/**
* This script saves a worksheet as a PDF and emails that PDF to a recipient.
*/
function main(workbook: ExcelScript.Workbook) {
// Create the PDF.
const pdf = OfficeScript.convertToPdf();
const pdfFile = { name: "report.pdf", content: pdf }; // Enter your desired PDF name here.

// Download the PDF.
OfficeScript.downloadFile(pdfFile);

// Email the PDF.
OfficeScript.sendMail({
to: "name@email.com", // Enter your recipient email address here.
subject: "[Demo] Monthly Sales Report",
content: "Here's the Monthly Sales Report",
attachments: [pdfFile]
})   
}
```
6 changes: 5 additions & 1 deletion docs/toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ items:
href: resources/samples/excel-calculation.md
- name: Move selected rows across tables
href: resources/samples/move-rows-across-tables.md
- name: Combine data from multiple worksheets
href: resources/samples/combine-data-from-multiple-worksheets.md
- name: Save and email as a PDF
href: resources/samples/save-and-email-as-pdf.md
- name: Notify people with comments
href: resources/samples/add-excel-comments.md
- name: Output table data as JSON
Expand All @@ -94,7 +98,7 @@ items:
href: resources/samples/external-fetch-calls.md
- name: Cross-application scenarios
items:
- name: Combine worksheets into a single workbook
- name: Combine worksheets with Power Automate
href: resources/samples/combine-worksheets-into-single-workbook.md
- name: Convert CSV files
href: resources/samples/convert-csv.md
Expand Down