|
1 | 1 | ---
|
2 | 2 | title: Convert CSV files to Excel workbooks
|
3 | 3 | 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 |
5 | 5 | ms.localizationpriority: medium
|
6 | 6 | ---
|
7 | 7 |
|
@@ -43,25 +43,26 @@ function main(workbook: ExcelScript.Workbook, csv: string) {
|
43 | 43 | const csvMatchRegex = /(?:,|\n|^)("(?:(?:"")*[^"]*)*"|[^",\n]*|(?:\n|$))/g
|
44 | 44 | rows.forEach((value, index) => {
|
45 | 45 | if (value.length > 0) {
|
46 |
| - let row = value.match(csvMatchRegex); |
| 46 | + let row = value.match(csvMatchRegex); |
47 | 47 |
|
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 | + }); |
61 | 58 |
|
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); |
65 | 66 | }
|
66 | 67 | });
|
67 | 68 |
|
@@ -186,3 +187,14 @@ If your file has hundreds of thousands of cells, you could reach the [Excel data
|
186 | 187 | 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).
|
187 | 188 |
|
188 | 189 | 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