Skip to content

Commit 85914f4

Browse files
authored
[excel] (Samples) Write large dataset clarifications (#641)
* Clarify instructions around two script blocks * Clean up script formatting * Fix link * New images
1 parent 51c45e2 commit 85914f4

9 files changed

+35
-24
lines changed

docs/images/write-large-dataset-1.png

20.4 KB
Loading

docs/images/write-large-dataset-2.png

14.8 KB
Loading

docs/images/write-large-dataset-3.png

12.2 KB
Loading

docs/images/write-large-dataset-4.png

31.6 KB
Loading

docs/images/write-large-dataset-5.png

35.3 KB
Loading

docs/images/write-large-dataset-6.png

14.4 KB
Loading

docs/images/write-large-dataset-7.png

9.76 KB
Loading

docs/images/write-large-dataset-8.png

22.1 KB
Loading

docs/resources/samples/write-large-dataset.md

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: Write a large dataset
33
description: Learn how to split a large dataset into smaller write operations in Office Scripts.
4-
ms.date: 02/24/2023
4+
ms.date: 09/07/2023
55
ms.localizationpriority: medium
66
---
77

@@ -161,33 +161,42 @@ For this sample, you'll need to complete the following steps.
161161
### Sample code: Read part of a workbook
162162

163163
```TypeScript
164-
function main(workbook: ExcelScript.Workbook, startRow: number, batchSize: number) : string[][] {
165-
// This sample only reads the first worksheet in the workbook.
166-
const sheet = workbook.getWorksheets()[0];
167-
168-
// Get the boundaries of the range.
169-
// Note that we're assuming usedRange is too big to read or write as a single range.
170-
const usedRange = sheet.getUsedRange();
171-
const lastColumnIndex = usedRange.getLastColumn().getColumnIndex();
172-
const lastRowindex = usedRange.getLastRow().getRowIndex();
173-
174-
// If we're starting past the last row, exit the script.
175-
if (startRow > lastRowindex) {
176-
return [[]];
177-
}
164+
function main(
165+
workbook: ExcelScript.Workbook,
166+
startRow: number,
167+
batchSize: number
168+
): string[][] {
169+
// This sample only reads the first worksheet in the workbook.
170+
const sheet = workbook.getWorksheets()[0];
171+
172+
// Get the boundaries of the range.
173+
// Note that we're assuming usedRange is too big to read or write as a single range.
174+
const usedRange = sheet.getUsedRange();
175+
const lastColumnIndex = usedRange.getLastColumn().getColumnIndex();
176+
const lastRowindex = usedRange.getLastRow().getRowIndex();
178177

179-
// Get the next batch or the rest of the rows, whichever is smaller.
180-
const rowCountToRead = Math.min(batchSize, (lastRowindex - startRow + 1));
181-
const rangeToRead = sheet.getRangeByIndexes(startRow, 0, rowCountToRead, lastColumnIndex + 1);
182-
return rangeToRead.getValues() as string[][];
178+
// If we're starting past the last row, exit the script.
179+
if (startRow > lastRowindex) {
180+
return [[]];
181+
}
182+
183+
// Get the next batch or the rest of the rows, whichever is smaller.
184+
const rowCountToRead = Math.min(batchSize, (lastRowindex - startRow + 1));
185+
const rangeToRead = sheet.getRangeByIndexes(startRow, 0, rowCountToRead, lastColumnIndex + 1);
186+
return rangeToRead.getValues() as string[][];
183187
}
184188

185189
```
186190

187191
### Sample code: Write part of a workbook
188192

189193
```TypeScript
190-
function main(workbook: ExcelScript.Workbook, data: string[][], currentRow: number, batchSize: number): boolean {
194+
function main(
195+
workbook: ExcelScript.Workbook,
196+
data: string[][],
197+
currentRow: number,
198+
batchSize: number
199+
): boolean {
191200
// Get the first worksheet.
192201
const sheet = workbook.getWorksheets()[0];
193202

@@ -196,7 +205,7 @@ function main(workbook: ExcelScript.Workbook, data: string[][], currentRow: numb
196205
sheet.getRangeByIndexes(currentRow, 0, data.length, data[0].length).setValues(data);
197206
}
198207

199-
// If we wrote less data than the batch size, signal the end of the flow.
208+
// If the script wrote less data than the batch size, signal the end of the flow.
200209
return batchSize > data.length;
201210
}
202211
```
@@ -223,7 +232,7 @@ function main(workbook: ExcelScript.Workbook, data: string[][], currentRow: numb
223232
* **Choose a value**: -1
224233

225234
:::image type="content" source="../../images/write-large-dataset-3.png" alt-text="The completed 'Do until' control.":::
226-
1. The remaining steps are added inside the **Do until** control. Next, call the script to read the data. Add an **Excel Online (Business)** connector with the **Run script** action. Use the following values for the action.
235+
1. The remaining steps are added inside the **Do until** control. Next, call the script to read the data. Add an **Excel Online (Business)** connector with the **Run script** action. Rename it to **Read data**. Use the following values for the action.
227236
* **Location**: OneDrive for Business
228237
* **Document Library**: OneDrive
229238
* **File**: "SampleData.xlsx" (as selected by the file picker)
@@ -232,11 +241,13 @@ function main(workbook: ExcelScript.Workbook, data: string[][], currentRow: numb
232241
* **batchSize**: *batchSize* (dynamic content)
233242

234243
:::image type="content" source="../../images/write-large-dataset-4.png" alt-text="The completed 'Run script' action for the script that reads the data.":::
235-
1. Call the script to write the data. Add a second **Excel Online (Business)** connector with the **Run script** action. Use the following values for the action.
244+
1. Call the script to write the data. Add a second **Excel Online (Business)** connector with the **Run script** action. Rename it to **Write data**. Use the following values for the action.
236245
* **Location**: OneDrive for Business
237246
* **Document Library**: OneDrive
238247
* **File**: "TargetWorkbook.xlsx" (as selected by the file picker)
239248
* **Script**: Write data at row location
249+
* **data**: *result* (dynamic content from **Read data**)
250+
* Press **[Switch input to entire array](../../testing/power-automate-troubleshooting.md#pass-entire-arrays-as-script-parameters)** first.
240251
* **startRow**: *currentRow* (dynamic content)
241252
* **batchSize**: *batchSize* (dynamic content)
242253

@@ -247,7 +258,7 @@ function main(workbook: ExcelScript.Workbook, data: string[][], currentRow: numb
247258

248259
:::image type="content" source="../../images/write-large-dataset-6.png" alt-text="The completed 'Increment variable' step for the 'currentRow'.":::
249260
1. Add a **Condition** control to check if the scripts have read everything. The "Write data at row location" script returns true when it has written fewer rows than the batch size allows. This means it's at the end of the data set. Create the **Condition** control with the following values.
250-
* **Choose a value**: *result* (dynamic content from **Run script**)
261+
* **Choose a value**: *result* (dynamic content from **Write data**)
251262
* **is equal to** (from the dropdown list)
252263
* **Choose a value**: *true* (expression)
253264

0 commit comments

Comments
 (0)