@@ -1039,71 +1039,90 @@ it.each(["utf-16le", "utf-16be", "windows-1252"])(
10391039) ;
10401040
10411041it . each ( [
1042- [ "" , "worksheets/./sheet1.xml" , false ] ,
1043- [ "x:" , "../xl/worksheets/sheet1.xml" , false ] ,
1044- [ "" , "/xl/worksheets/sheet1.xml" , true ] ,
1045- ] ) ( "preserves %sworksheet metadata with relationship target %s" , async ( prefix , target , empty ) => {
1046- const { default : JSZip } = await import ( "jszip" ) ;
1047- const archive = new JSZip ( ) ;
1048- const ns = "http://schemas.openxmlformats.org/spreadsheetml/2006/main" ;
1049- archive . file (
1050- "xl/workbook.xml" ,
1051- '<workbook xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"><sheets><sheet name="Sheet" sheetId="1" r:id="s"/></sheets></workbook>' ,
1052- ) ;
1053- archive . file (
1054- "xl/_rels/workbook.xml.rels" ,
1055- `<Relationships><Relationship Id="s" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="${ target } "/></Relationships>` ,
1056- ) ;
1057- archive . file (
1058- "xl/worksheets/sheet1.xml" ,
1059- `<${ prefix } worksheet xmlns${ prefix ? ":x" : "" } ="${ ns } "><${ prefix } dimension ref="XFD1048576"/><${ prefix } cols><${ prefix } col min="${ OFFICE_PREVIEW_MAX_COLUMNS - 1 } " max="${ OFFICE_PREVIEW_MAX_COLUMNS + 10 } " width="20"/><${ prefix } col min="${ OFFICE_PREVIEW_MAX_COLUMNS + 1 } " max="${ OFFICE_PREVIEW_MAX_COLUMNS + 30 } " hidden="1"/></${ prefix } cols><${ prefix } sheetData><${ prefix } row ht="42" customHeight="1" hidden="1"><${ prefix } c r="A50" t="inlineStr"><${ prefix } is><${ prefix } t>${ "value" . repeat ( 1000 ) } </${ prefix } t></${ prefix } is></${ prefix } c></${ prefix } row></${ prefix } sheetData><${ prefix } mergeCells><${ prefix } mergeCell ref="XFD1048576"/></${ prefix } mergeCells></${ prefix } worksheet>`
1060- . replace ( / < ( (?: x : ) ? (?: c o l | d i m e n s i o n | m e r g e C e l l ) ) \b ( [ ^ > ] * ) \/ > / g, prefix ? "<$1$2></$1>" : "$&" )
1061- . replace ( / < s h e e t D a t a > [ \s \S ] * ?< \/ s h e e t D a t a > / , ( data ) => ( empty ? "<sheetData/>" : data ) )
1062- . replace ( / = " ( [ ^ " ] * ) " / g, ( attribute , value : string ) =>
1063- prefix || empty
1064- ? ` = '${ value . replace ( / \d / g, ( digit ) => `&#${ digit . charCodeAt ( 0 ) } ;` ) } '`
1065- : attribute ,
1066- ) ,
1067- ) ;
1068- const bytes = await archive . generateAsync ( { type : "arraybuffer" } ) ;
1069- const worker = new Worker ( new URL ( "./officePreview.worker.ts" , import . meta. url ) , {
1070- type : "module" ,
1071- } ) ;
1072- try {
1073- const result = await new Promise < OfficeWorkerResult > ( ( resolve ) => {
1074- worker . addEventListener (
1075- "message" ,
1076- ( event : MessageEvent < OfficeWorkerResult > ) => resolve ( event . data ) ,
1077- { once : true } ,
1078- ) ;
1079- worker . postMessage ( { type : "open" , kind : "xlsx" , bytes } , [ bytes ] ) ;
1080- } ) ;
1081- expect ( result . type ) . toBe ( "xlsx" ) ;
1082- if ( result . type !== "xlsx" ) throw new Error ( "Expected bounded workbook" ) ;
1083- const bounded = await JSZip . loadAsync ( result . bytes ) ;
1084- const sheet = new DOMParser ( ) . parseFromString (
1085- await bounded . file ( "xl/worksheets/sheet1.xml" ) ! . async ( "string" ) ,
1086- "application/xml" ,
1042+ [ "" , "worksheets/./sheet1.xml" , false , false ] ,
1043+ [ "x:" , "../xl/worksheets/sheet1.xml" , false , false ] ,
1044+ [ "" , "/xl/worksheets/sheet1.xml" , true , false ] ,
1045+ [ "" , "worksheets/sheet1.xml" , false , true ] ,
1046+ [ "" , "worksheets/sheet1.xml" , true , true ] ,
1047+ ] ) (
1048+ "preserves %sworksheet metadata with relationship target %s" ,
1049+ async ( prefix , target , empty , localPrefix ) => {
1050+ const { default : JSZip } = await import ( "jszip" ) ;
1051+ const archive = new JSZip ( ) ;
1052+ const ns = "http://schemas.openxmlformats.org/spreadsheetml/2006/main" ;
1053+ archive . file (
1054+ "xl/workbook.xml" ,
1055+ '<workbook xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"><sheets><sheet name="Chart" sheetId="2" r:id="c"/><sheet name="Sheet" sheetId="1" r:id="s"/></sheets></workbook>' ,
10871056 ) ;
1088- expect ( sheet . querySelector ( "parsererror" ) ) . toBeNull ( ) ;
1089- const row = sheet . getElementsByTagNameNS ( ns , "row" ) [ 0 ] ;
1090- if ( empty ) expect ( row ) . toBeUndefined ( ) ;
1091- else {
1092- expect ( row ! . getAttribute ( "ht" ) ) . toBe ( "42" ) ;
1093- expect ( row ! . getAttribute ( "hidden" ) ) . toBe ( "1" ) ;
1094- expect ( row ! . getElementsByTagNameNS ( ns , "c" ) [ 0 ] ! . getAttribute ( "r" ) ) . toBe ( "A50" ) ;
1095- expect ( row ! . getElementsByTagNameNS ( ns , "t" ) [ 0 ] ! . textContent ) . toHaveLength ( 4096 ) ;
1057+ archive . file (
1058+ "xl/_rels/workbook.xml.rels" ,
1059+ `<Relationships><Relationship Id="s" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="${ target } "/><Relationship Id="c" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/chartsheet" Target="chartsheets/sheet1.xml"/></Relationships>` ,
1060+ ) ;
1061+ archive . file (
1062+ "xl/worksheets/sheet1.xml" ,
1063+ `<${ prefix } worksheet xmlns${ prefix ? ":x" : "" } ="${ ns } "><${ prefix } dimension ref="XFD1048576"/><${ prefix } sheetViews><${ prefix } sheetView showGridLines="0"/></${ prefix } sheetViews><${ prefix } sheetFormatPr defaultColWidth="30"/><${ prefix } cols><${ prefix } col min="${ OFFICE_PREVIEW_MAX_COLUMNS - 1 } " max="${ OFFICE_PREVIEW_MAX_COLUMNS + 10 } " width="20"/><${ prefix } col min="${ OFFICE_PREVIEW_MAX_COLUMNS + 1 } " max="${ OFFICE_PREVIEW_MAX_COLUMNS + 30 } " hidden="1"/></${ prefix } cols><${ prefix } sheetData><${ prefix } row ht="42" customHeight="1" hidden="1"><${ prefix } c r="A50" t="inlineStr"><${ prefix } is><${ prefix } t>${ "value" . repeat ( 1000 ) } </${ prefix } t></${ prefix } is></${ prefix } c></${ prefix } row></${ prefix } sheetData><${ prefix } mergeCells><${ prefix } mergeCell ref="XFD1048576"/></${ prefix } mergeCells></${ prefix } worksheet>`
1064+ . replace (
1065+ / < ( (?: x : ) ? (?: c o l | d i m e n s i o n | m e r g e C e l l ) ) \b ( [ ^ > ] * ) \/ > / g,
1066+ prefix ? "<$1$2></$1>" : "$&" ,
1067+ )
1068+ . replace ( / < s h e e t D a t a > [ \s \S ] * ?< \/ s h e e t D a t a > / , ( data ) => ( empty ? "<sheetData/>" : data ) )
1069+ . replace ( / < ( \/ ? ) s h e e t D a t a \b / g, ( tag , closing : string ) =>
1070+ localPrefix ? `<${ closing } x:sheetData${ closing ? "" : ` xmlns:x="${ ns } "` } ` : tag ,
1071+ )
1072+ . replace ( / = " ( [ ^ " ] * ) " / g, ( attribute , value : string ) =>
1073+ prefix || empty
1074+ ? ` = '${ value . replace ( / \d / g, ( digit ) => `&#${ digit . charCodeAt ( 0 ) } ;` ) } '`
1075+ : attribute ,
1076+ ) ,
1077+ ) ;
1078+ const bytes = await archive . generateAsync ( { type : "arraybuffer" } ) ;
1079+ const worker = new Worker ( new URL ( "./officePreview.worker.ts" , import . meta. url ) , {
1080+ type : "module" ,
1081+ } ) ;
1082+ try {
1083+ const result = await new Promise < OfficeWorkerResult > ( ( resolve ) => {
1084+ worker . addEventListener (
1085+ "message" ,
1086+ ( event : MessageEvent < OfficeWorkerResult > ) => resolve ( event . data ) ,
1087+ { once : true } ,
1088+ ) ;
1089+ worker . postMessage ( { type : "open" , kind : "xlsx" , bytes } , [ bytes ] ) ;
1090+ } ) ;
1091+ expect ( result . type ) . toBe ( "xlsx" ) ;
1092+ if ( result . type !== "xlsx" ) throw new Error ( "Expected bounded workbook" ) ;
1093+ // The engine counts the chart sheet before it.
1094+ expect ( result . view ?. hiddenGridlines ) . toEqual ( [ 1 ] ) ;
1095+ const bounded = await JSZip . loadAsync ( result . bytes ) ;
1096+ const sheet = new DOMParser ( ) . parseFromString (
1097+ await bounded . file ( "xl/worksheets/sheet1.xml" ) ! . async ( "string" ) ,
1098+ "application/xml" ,
1099+ ) ;
1100+ expect ( sheet . querySelector ( "parsererror" ) ) . toBeNull ( ) ;
1101+ const row = sheet . getElementsByTagNameNS ( ns , "row" ) [ 0 ] ;
1102+ if ( empty ) expect ( row ) . toBeUndefined ( ) ;
1103+ else {
1104+ expect ( row ! . getAttribute ( "ht" ) ) . toBe ( "42" ) ;
1105+ expect ( row ! . getAttribute ( "hidden" ) ) . toBe ( "1" ) ;
1106+ expect ( row ! . getElementsByTagNameNS ( ns , "c" ) [ 0 ] ! . getAttribute ( "r" ) ) . toBe ( "A50" ) ;
1107+ expect ( row ! . getElementsByTagNameNS ( ns , "t" ) [ 0 ] ! . textContent ) . toHaveLength ( 4096 ) ;
1108+ }
1109+ expect ( sheet . getElementsByTagNameNS ( ns , "dimension" ) ) . toHaveLength ( 0 ) ;
1110+ expect ( sheet . getElementsByTagNameNS ( ns , "mergeCell" ) ) . toHaveLength ( 0 ) ;
1111+ const columns = [ ...sheet . getElementsByTagNameNS ( ns , "col" ) ] . map ( ( column ) =>
1112+ [ "min" , "max" , "width" ] . map ( ( name ) => column . getAttribute ( name ) ) ,
1113+ ) ;
1114+ // The engine ignores `defaultColWidth`, so the columns it sizes are spelled out.
1115+ expect ( columns ) . toEqual ( [
1116+ [ String ( OFFICE_PREVIEW_MAX_COLUMNS - 1 ) , String ( OFFICE_PREVIEW_MAX_COLUMNS ) , "20" ] ,
1117+ // Hidden columns keep their span past the limit, so they stay hidden.
1118+ [ String ( OFFICE_PREVIEW_MAX_COLUMNS + 1 ) , String ( OFFICE_PREVIEW_MAX_COLUMNS + 30 ) , null ] ,
1119+ [ "1" , String ( OFFICE_PREVIEW_MAX_COLUMNS - 2 ) , "30" ] ,
1120+ ] ) ;
1121+ } finally {
1122+ worker . terminate ( ) ;
10961123 }
1097- expect ( sheet . getElementsByTagNameNS ( ns , "dimension" ) ) . toHaveLength ( 0 ) ;
1098- expect ( sheet . getElementsByTagNameNS ( ns , "mergeCell" ) ) . toHaveLength ( 0 ) ;
1099- const columns = sheet . getElementsByTagNameNS ( ns , "col" ) ;
1100- expect ( columns ) . toHaveLength ( 1 ) ;
1101- expect ( columns [ 0 ] ! . getAttribute ( "min" ) ) . toBe ( String ( OFFICE_PREVIEW_MAX_COLUMNS - 1 ) ) ;
1102- expect ( columns [ 0 ] ! . getAttribute ( "max" ) ) . toBe ( String ( OFFICE_PREVIEW_MAX_COLUMNS ) ) ;
1103- } finally {
1104- worker . terminate ( ) ;
1105- }
1106- } ) ;
1124+ } ,
1125+ ) ;
11071126
11081127it ( "reports the final slide at maximum scroll after page visibility changes" , async ( ) => {
11091128 const { OfficePages } = await import ( "./office/OfficePages" ) ;
@@ -1717,7 +1736,7 @@ it.each(["repeated binary", "XML relationship alias", "duplicate relationship ID
17171736 } ,
17181737) ;
17191738
1720- it . each ( [ "XFD200" , "XFD1048576" , "AX500" , "AZ9999999" ] ) (
1739+ it . each ( [ "XFD200" , "XFD300" , " XFD1048576", "AX500" , "AZ9999999" ] ) (
17211740 "bounds spreadsheet cells at %s before padding" ,
17221741 async ( ref ) => {
17231742 const { default : JSZip } = await import ( "jszip" ) ;
@@ -1733,7 +1752,7 @@ it.each(["XFD200", "XFD1048576", "AX500", "AZ9999999"])(
17331752 ) ;
17341753 archive . file (
17351754 "xl/custom.xml" ,
1736- `<worksheet><dimension ref="A1:${ ref } "/><sheetData><row r="1"><c r="A1"><v>7</v></c></row><row r="${ ref . replace ( / \D / g, "" ) } "><c r="${ ref } ">< v>9</v></c></row></sheetData></worksheet>` ,
1755+ `<worksheet><dimension ref="A1:${ ref } "/><sheetData><row r="1"><c r="A1"><v>7</v></c></row><row r="${ ref . replace ( / \D / g, "" ) } ">${ ref === "XFD300" ? ` <c r="${ ref } " s="1"/>` : `<c r=" ${ ref } ">< v>9</v></c>` } </row></sheetData></worksheet>` ,
17371756 ) ;
17381757 const bytes = await archive . generateAsync ( { type : "arraybuffer" } ) ;
17391758 const worker = new Worker ( new URL ( "./officePreview.worker.ts" , import . meta. url ) , {
@@ -1763,7 +1782,7 @@ it.each(["XFD200", "XFD1048576", "AX500", "AZ9999999"])(
17631782 ) ;
17641783 expect ( sheet . querySelector ( 'c[r="A1"] v' ) ?. textContent ) . toBe ( "7" ) ;
17651784 expect ( sheet . querySelectorAll ( "row" ) . length ) . toBeLessThanOrEqual ( 500 ) ;
1766- expect ( result . truncated ) . toBe ( ref !== "AX500 ") ;
1785+ expect ( result . truncated ) . toBe ( ref === "XFD200" || ref === "XFD300 ") ;
17671786 if ( ref === "AX500" ) expect ( sheet . querySelector ( 'c[r="AX500"] v' ) ?. textContent ) . toBe ( "9" ) ;
17681787 else expect ( sheet . querySelector ( 'c[r="' + ref + '"]' ) ) . toBeNull ( ) ;
17691788 }
@@ -1773,6 +1792,62 @@ it.each(["XFD200", "XFD1048576", "AX500", "AZ9999999"])(
17731792 } ,
17741793) ;
17751794
1795+ it . each ( [ '<u val="double"/>' , '<u val="double"></u>' ] ) (
1796+ "shows workbook formats and underline %s" ,
1797+ async ( underline ) => {
1798+ const { default : JSZip } = await import ( "jszip" ) ;
1799+ const archive = new JSZip ( ) ;
1800+ archive . file (
1801+ "xl/workbook.xml" ,
1802+ '<workbook xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"><sheets><sheet name="Styles" sheetId="1" r:id="rId1"/></sheets></workbook>' ,
1803+ ) ;
1804+ archive . file (
1805+ "xl/_rels/workbook.xml.rels" ,
1806+ '<Relationships><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet" Target="worksheets/sheet1.xml"/></Relationships>' ,
1807+ ) ;
1808+ archive . file (
1809+ "xl/styles.xml" ,
1810+ `<styleSheet><numFmts count="4"><numFmt numFmtId="164" formatCode="0.000"/><numFmt numFmtId="65535" formatCode="0.0000"/><numFmt numFmtId="007" formatCode=""¥"#,##0.00"/><numFmt numFmtId="82" formatCode="[$-010409]dd-mmm-yyyy"/></numFmts><fonts count="2"><font><sz val="11"/></font><font>${ underline } <sz val="11"/></font></fonts><cellXfs count="4"><xf numFmtId="0" fontId="0"/><xf numFmtId="7" fontId="0"/><xf numFmtId="82" fontId="0"/><xf numFmtId="0" fontId="1"/></cellXfs></styleSheet>` ,
1811+ ) ;
1812+ archive . file (
1813+ "xl/worksheets/sheet1.xml" ,
1814+ '<worksheet><sheetData><row r="1"><c r="A1" s="1"><v>100</v></c><c r="B1" s="2"><v>44986</v></c><c r="C1" s="3" t="inlineStr"><is><t>Total</t></is></c></row></sheetData></worksheet>' ,
1815+ ) ;
1816+ const { prepareOffice } = await import ( "./officeArchive" ) ;
1817+ const prepared = await prepareOffice (
1818+ await archive . generateAsync ( { type : "arraybuffer" } ) ,
1819+ "xlsx" ,
1820+ ) ;
1821+ const styles = await (
1822+ await JSZip . loadAsync ( prepared . bytes )
1823+ )
1824+ . file ( "xl/styles.xml" ) !
1825+ . async ( "string" ) ;
1826+ expect ( styles ) . toContain ( "<font><u/>" ) ;
1827+ const worker = new Worker ( new URL ( "./office/spreadsheet.worker.ts" , import . meta. url ) , {
1828+ type : "module" ,
1829+ } ) ;
1830+ try {
1831+ const { createWorkerClient } = await import ( "./office/worker-rpc" ) ;
1832+ const client = createWorkerClient < import ( "./office/sheet-protocol" ) . SheetMethods > ( worker ) ;
1833+ const bytes = prepared . bytes ;
1834+ await client . call ( "open" , { bytes, view : prepared . view ! } , { transfer : [ bytes ] } ) ;
1835+ const cells = await client . call ( "cells" , {
1836+ sheet : 0 ,
1837+ range : { top : 0 , bottom : 0 , left : 0 , right : 1 } ,
1838+ } ) ;
1839+ // Ids under 164 that a workbook defines keep its formats, not Excel's built-in ones.
1840+ expect ( cells [ 0 ] ) . toEqual ( [ "¥100.00" , "01-Mar-2023" ] ) ;
1841+ } finally {
1842+ worker . terminate ( ) ;
1843+ }
1844+ archive . file ( "xl/styles.xml" , "<styleSheet><numFmts/><numFmts/></styleSheet>" ) ;
1845+ await expect (
1846+ prepareOffice ( await archive . generateAsync ( { type : "arraybuffer" } ) , "xlsx" ) ,
1847+ ) . rejects . toThrow ( "duplicate stylesheet blocks" ) ;
1848+ } ,
1849+ ) ;
1850+
17761851it ( "keeps spreadsheet dates on their calendar day west of UTC" , async ( ) => {
17771852 const { default : JSZip } = await import ( "jszip" ) ;
17781853 const archive = new JSZip ( ) ;
@@ -1831,7 +1906,14 @@ it("keeps spreadsheet dates on their calendar day west of UTC", async () => {
18311906 } ) ;
18321907 const { createWorkerClient } = await import ( "./office/worker-rpc" ) ;
18331908 const client = createWorkerClient < import ( "./office/sheet-protocol" ) . SheetMethods > ( worker ) ;
1834- await client . call ( "open" , { bytes } , { transfer : [ bytes ] } ) ;
1909+ await client . call (
1910+ "open" ,
1911+ {
1912+ bytes,
1913+ view : { hiddenGridlines : [ ] , themeFonts : { major : "Calibri Light" , minor : "Calibri" } } ,
1914+ } ,
1915+ { transfer : [ bytes ] } ,
1916+ ) ;
18351917 const cells = await client . call ( "cells" , {
18361918 sheet : 0 ,
18371919 range : { top : 0 , bottom : 0 , left : 0 , right : 0 } ,
0 commit comments