Skip to content

Commit 8b68a1a

Browse files
authored
[excel] (CSV) Update sample to remove quotation marks around values (#705)
* Change CSV parser to remove surrounding quotation marks * Fix typo * Add missing comma * Fix typo
1 parent 2d5bb1c commit 8b68a1a

File tree

1 file changed

+30
-18
lines changed

1 file changed

+30
-18
lines changed

docs/resources/samples/convert-csv.md

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: Convert CSV files to Excel workbooks
33
description: Learn how to use Office Scripts and Power Automate to create .xlsx files from .csv files.
4-
ms.date: 01/04/2024
4+
ms.date: 01/17/2024
55
ms.localizationpriority: medium
66
---
77

@@ -43,25 +43,26 @@ function main(workbook: ExcelScript.Workbook, csv: string) {
4343
const csvMatchRegex = /(?:,|\n|^)("(?:(?:"")*[^"]*)*"|[^",\n]*|(?:\n|$))/g
4444
rows.forEach((value, index) => {
4545
if (value.length > 0) {
46-
let row = value.match(csvMatchRegex);
46+
let row = value.match(csvMatchRegex);
4747

48-
// Check for blanks at the start of the row.
49-
if (row[0].charAt(0) === ',') {
50-
row.unshift("");
51-
}
52-
53-
// Remove the preceding comma.
54-
row.forEach((cell, index) => {
55-
row[index] = cell.indexOf(",") === 0 ? cell.substr(1) : cell;
56-
});
57-
58-
// Create a 2D array with one row.
59-
let data: string[][] = [];
60-
data.push(row);
48+
// Check for blanks at the start of the row.
49+
if (row[0].charAt(0) === ',') {
50+
row.unshift("");
51+
}
52+
53+
// Remove the preceding comma and surrounding quotation marks.
54+
row.forEach((cell, index) => {
55+
cell = cell.indexOf(",") === 0 ? cell.substring(1) : cell;
56+
row[index] = cell.indexOf("\"") === 0 && cell.lastIndexOf("\"") === cell.length - 1 ? cell.substring(1, cell.length - 1) : cell;
57+
});
6158

62-
// Put the data in the worksheet.
63-
let range = sheet.getRangeByIndexes(index, 0, 1, data[0].length);
64-
range.setValues(data);
59+
// Create a 2D array with one row.
60+
let data: string[][] = [];
61+
data.push(row);
62+
63+
// Put the data in the worksheet.
64+
let range = sheet.getRangeByIndexes(index, 0, 1, data[0].length);
65+
range.setValues(data);
6566
}
6667
});
6768

@@ -186,3 +187,14 @@ If your file has hundreds of thousands of cells, you could reach the [Excel data
186187
Files with unicode-specific characters, such as accented vowels like `é`, need to be saved with the correct encoding. Power Automate's OneDrive connector file creation defaults to ANSI for .csv files. If you're creating the .csv files in Power Automate, you'll need to add the [byte order mark (BOM)](https://en.wikipedia.org/wiki/Byte_order_mark) before the comma-separated values. For UTF-8, replace the file contents for the write .csv file operation with the expression `concat(uriComponentToString('%EF%BB%BF'), <CSV Input>)` (where `<CSV Input>` is your original CSV data).
187188

188189
Note that this sample doesn't create the .csv files in the flow, so this change needs to happen in your custom part of the flow. You could also read and rewrite the .csv files with the BOM, if you don't control how those files are created.
190+
191+
### Surrounding quotation marks
192+
193+
This sample removes any quotation marks ("") that surround values. These are typically added to comma-separated values to prevent commas in the data from being treated as separation tokens. A .csv file that is opened in Excel, then saved as a .xlsx file, will never have the those quotation marks shown to the reader. If you wish to keep the quotation marks and have them be displayed in the final spreadsheets, replace lines 27-30 of the script with the following code.
194+
195+
```typescript
196+
// Remove the preceding comma.
197+
row.forEach((cell, index) => {
198+
row[index] = cell.indexOf(",") === 0 ? cell.substring(1) : cell;
199+
});
200+
```

0 commit comments

Comments
 (0)