Last active
April 25, 2025 17:14
-
-
Save ninmonkey/ab33afa22bed844923267de4b9b15a59 to your computer and use it in GitHub Desktop.
Saving-Excel-Workbook-As-Base64-Round-Trip.pq
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let | |
| Path_Xlsx = "c:\data\workbook.xlsx", | |
| Summary = [ | |
| // [1] using "enter-data" saves a table as json like this | |
| b64_str = "i45WSkosUoqNBQA=", | |
| bytes = Binary.FromText( b64_str, BinaryEncoding.Base64), | |
| bytes_decompressed = Binary.Decompress( bytes, Compression.Deflate), | |
| final_json_str = Text.FromBinary( bytes_decompressed, TextEncoding.Utf8 ), | |
| // [2] converting some binary file to base64 | |
| xl_bytes = File.Contents( Path_Xlsx ), | |
| xl_compress = Binary.Compress( xl_bytes , Compression.Deflate ), | |
| xl_b64_str = Binary.ToText( xl_compress, BinaryEncoding.Base64 ), // Your str literal | |
| // [3] decode as bytes and verify the table is valid | |
| rt_bytes = Binary.FromText( xl_b64_str, BinaryEncoding.Base64 ), | |
| rt_unzip = Binary.Decompress( rt_bytes, Compression.Deflate ), | |
| rt_book = Excel.Workbook( rt_unzip, null, false ), | |
| book_source = Excel.Workbook( File.Contents( Path_Xlsx ) ), | |
| valid_round_trip = book_source = rt_book | |
| ] | |
| in | |
| Summary |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| let | |
| Path_Xlsx = "c:\data\workbook.xlsx", | |
| Summary = [ | |
| path = Path_Xlsx, | |
| b64_str = "i45WSkosUoqNBQA=", | |
| bytes = Binary.FromText( b64_str, BinaryEncoding.Base64), | |
| decompressed = Binary.Decompress( bytes, Compression.Deflate), | |
| str = Text.FromBinary( decompressed, TextEncoding.Utf8 ), | |
| xl_bytes = File.Contents( Path_Xlsx ), | |
| xl_compress = Binary.Compress( xl_bytes , Compression.Deflate ), | |
| xl_b64_str = Binary.ToText( xl_compress, BinaryEncoding.Base64 ), // Your str literal | |
| // round trip test | |
| rt_bytes = Binary.FromText( xl_b64_str, BinaryEncoding.Base64 ), | |
| rt_unzip = Binary.Decompress( rt_bytes, Compression.Deflate ), | |
| book = Excel.Workbook( rt_unzip, null, false ), | |
| // z = Text.FromBinary(rt_bytes), | |
| // z2 = Text.FromBinary(rt_unzip), | |
| raw_str = Text.FromBinary( File.Contents( Path_Xlsx )), | |
| book_source = Excel.Workbook( File.Contents( Path_Xlsx ) ), | |
| valid_round_trip = book_source = book, | |
| shouldFail = Excel.Workbook( rt_bytes ), | |
| shouldWork = Excel.Workbook( rt_unzip ), | |
| errorMessage = try shouldFail catch (e) => e[Message], | |
| errorRecord = try shouldFail catch (e) => e meta [ ErrRecord = e ], | |
| errMeta = Value.Metadata( errorRecord ), | |
| // converts most things into json representation | |
| Json = (target as any) as text => | |
| Text.FromBinary( Json.FromValue( target ) ), | |
| errorAsText = Json( errorRecord ), | |
| errorAsText_extraMetadata = Json( errMeta ) | |
| ] | |
| in | |
| Summary |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment