@@ -32,8 +32,7 @@ const commands: CliCommand[] = [
3232  { 
3333    name : 'render' , 
3434    description : 'Render OSF to various output formats' , 
35-     usage :
36-       'osf render <file> [--format <html|pdf|docx|pptx|xlsx>] [--output <file>]' , 
35+     usage : 'osf render <file> [--format <html|pdf|docx|pptx|xlsx>] [--output <file>]' , 
3736    args : [ 'file' ] , 
3837  } , 
3938  { 
@@ -149,10 +148,10 @@ class FormulaEvaluator {
149148
150149    // Replace cell references with actual values 
151150    const  cellRefRegex  =  / \b ( [ A - Z ] + \d + ) \b / g; 
152-     const  processedExpr  =  expr . replace ( cellRefRegex ,  ( match )  =>  { 
151+     const  processedExpr  =  expr . replace ( cellRefRegex ,  match  =>  { 
153152      const  [ row ,  col ]  =  this . cellRefToCoords ( match ) ; 
154153      const  value  =  this . getCellValue ( row ,  col ) ; 
155-        
154+ 
156155      // Convert to number if possible, otherwise use as string 
157156      if  ( typeof  value  ===  'number' )  { 
158157        return  value . toString ( ) ; 
@@ -181,14 +180,14 @@ class FormulaEvaluator {
181180    while  ( expr . includes ( '(' ) )  { 
182181      const  innerMatch  =  expr . match ( / \( ( [ ^ ( ) ] + ) \) / ) ; 
183182      if  ( ! innerMatch )  break ; 
184-        
183+ 
185184      const  innerResult  =  this . evaluateExpression ( innerMatch [ 1 ] ! ) ; 
186185      expr  =  expr . replace ( innerMatch [ 0 ] ! ,  innerResult . toString ( ) ) ; 
187186    } 
188187
189188    // Handle multiplication and division (left to right, same precedence) 
190189    expr  =  this . evaluateOperatorsLeftToRight ( expr ,  [ '*' ,  '/' ] ) ; 
191-      
190+ 
192191    // Handle addition and subtraction (left to right, same precedence) 
193192    expr  =  this . evaluateOperatorsLeftToRight ( expr ,  [ '+' ,  '-' ] ) ; 
194193
@@ -197,7 +196,7 @@ class FormulaEvaluator {
197196    if  ( isNaN ( result ) )  { 
198197      throw  new  Error ( `Invalid expression: ${ expr }  ) ; 
199198    } 
200-      
199+ 
201200    return  result ; 
202201  } 
203202
@@ -206,37 +205,44 @@ class FormulaEvaluator {
206205    // Create a regex that matches any of the operators 
207206    const  opPattern  =  operators . map ( op  =>  `\\${ op }  ) . join ( '|' ) ; 
208207    const  regex  =  new  RegExp ( `(-?\\d+(?:\\.\\d+)?)(${ opPattern }  ,  'g' ) ; 
209-      
208+ 
210209    // Keep evaluating until no more matches 
211210    while  ( regex . test ( expr ) )  { 
212211      regex . lastIndex  =  0 ;  // Reset regex 
213212      expr  =  expr . replace ( regex ,  ( _ ,  left ,  op ,  right )  =>  { 
214213        const  leftNum  =  parseFloat ( left ) ; 
215214        const  rightNum  =  parseFloat ( right ) ; 
216215        let  result : number ; 
217-          
216+ 
218217        switch  ( op )  { 
219-           case  '+' : result  =  leftNum  +  rightNum ;  break ; 
220-           case  '-' : result  =  leftNum  -  rightNum ;  break ; 
221-           case  '*' : result  =  leftNum  *  rightNum ;  break ; 
222-           case  '/' : 
218+           case  '+' :
219+             result  =  leftNum  +  rightNum ; 
220+             break ; 
221+           case  '-' :
222+             result  =  leftNum  -  rightNum ; 
223+             break ; 
224+           case  '*' :
225+             result  =  leftNum  *  rightNum ; 
226+             break ; 
227+           case  '/' :
223228            if  ( rightNum  ===  0 )  throw  new  Error ( 'Division by zero' ) ; 
224-             result  =  leftNum  /  rightNum ;   
229+             result  =  leftNum  /  rightNum ; 
225230            break ; 
226-           default : throw  new  Error ( `Unknown operator: ${ op }  ) ; 
231+           default :
232+             throw  new  Error ( `Unknown operator: ${ op }  ) ; 
227233        } 
228-          
234+ 
229235        return  result . toString ( ) ; 
230236      } ) ; 
231237    } 
232-      
238+ 
233239    return  expr ; 
234240  } 
235241
236242  // Get all computed values for a sheet 
237243  getAllComputedValues ( maxRow : number ,  maxCol : number ) : Record < string ,  any >  { 
238244    const  result : Record < string ,  any >  =  { } ; 
239-      
245+ 
240246    for  ( let  r  =  1 ;  r  <=  maxRow ;  r ++ )  { 
241247      for  ( let  c  =  1 ;  c  <=  maxCol ;  c ++ )  { 
242248        const  key  =  `${ r } ${ c }  ; 
@@ -251,7 +257,7 @@ class FormulaEvaluator {
251257        } 
252258      } 
253259    } 
254-      
260+ 
255261    return  result ; 
256262  } 
257263} 
@@ -402,15 +408,15 @@ function renderHtml(doc: OSFDocument): string {
402408        if  ( sheet . data )  { 
403409          // Evaluate formulas 
404410          const  evaluator  =  new  FormulaEvaluator ( sheet . data ,  sheet . formulas  ||  [ ] ) ; 
405-            
411+ 
406412          // Calculate dimensions including formula cells 
407413          const  dataCoords  =  Object . keys ( sheet . data ) . map ( k  =>  k . split ( ',' ) . map ( Number ) ) ; 
408414          const  formulaCoords  =  ( sheet . formulas  ||  [ ] ) . map ( ( f : any )  =>  f . cell ) ; 
409415          const  allCoords  =  [ ...dataCoords ,  ...formulaCoords ] ; 
410-            
416+ 
411417          const  maxRow  =  Math . max ( ...allCoords . map ( c  =>  c [ 0 ] ! ) ) ; 
412418          const  maxCol  =  Math . max ( ...allCoords . map ( c  =>  c [ 1 ] ! ) ) ; 
413-            
419+ 
414420          // Get all computed values including formulas 
415421          const  allValues  =  evaluator . getAllComputedValues ( maxRow ,  maxCol ) ; 
416422
@@ -420,12 +426,14 @@ function renderHtml(doc: OSFDocument): string {
420426            for  ( let  c  =  1 ;  c  <=  maxCol ;  c ++ )  { 
421427              const  key  =  `${ r } ${ c }  ; 
422428              const  val  =  allValues [ key ]  ??  '' ; 
423-               const  hasFormula  =  sheet . formulas ?. some ( ( f : any )  =>  f . cell [ 0 ]  ===  r  &&  f . cell [ 1 ]  ===  c ) ; 
429+               const  hasFormula  =  sheet . formulas ?. some ( 
430+                 ( f : any )  =>  f . cell [ 0 ]  ===  r  &&  f . cell [ 1 ]  ===  c 
431+               ) ; 
424432              const  isError  =  typeof  val  ===  'string'  &&  val . startsWith ( '#ERROR:' ) ; 
425-                
426-               const  cssClass  =  isError  ? 'error'  : ( hasFormula  ? 'computed'  : '' ) ; 
433+ 
434+               const  cssClass  =  isError  ? 'error'  : hasFormula  ? 'computed'  : '' ; 
427435              const  cellContent  =  isError  ? val . replace ( '#ERROR: ' ,  '' )  : val ; 
428-                
436+ 
429437              parts . push ( `        <td class="${ cssClass } ${ cellContent }  ) ; 
430438            } 
431439            parts . push ( '      </tr>' ) ; 
@@ -525,15 +533,15 @@ function exportMarkdown(doc: OSFDocument): string {
525533        if  ( sheet . data )  { 
526534          // Evaluate formulas 
527535          const  evaluator  =  new  FormulaEvaluator ( sheet . data ,  sheet . formulas  ||  [ ] ) ; 
528-            
536+ 
529537          // Calculate dimensions including formula cells 
530538          const  dataCoords  =  Object . keys ( sheet . data ) . map ( k  =>  k . split ( ',' ) . map ( Number ) ) ; 
531539          const  formulaCoords  =  ( sheet . formulas  ||  [ ] ) . map ( ( f : any )  =>  f . cell ) ; 
532540          const  allCoords  =  [ ...dataCoords ,  ...formulaCoords ] ; 
533-            
541+ 
534542          const  maxRow  =  Math . max ( ...allCoords . map ( c  =>  c [ 0 ] ! ) ) ; 
535543          const  maxCol  =  Math . max ( ...allCoords . map ( c  =>  c [ 1 ] ! ) ) ; 
536-            
544+ 
537545          // Get all computed values including formulas 
538546          const  allValues  =  evaluator . getAllComputedValues ( maxRow ,  maxCol ) ; 
539547
@@ -542,8 +550,10 @@ function exportMarkdown(doc: OSFDocument): string {
542550            for  ( let  c  =  1 ;  c  <=  maxCol ;  c ++ )  { 
543551              const  key  =  `${ r } ${ c }  ; 
544552              const  val  =  allValues [ key ]  ??  '' ; 
545-               const  hasFormula  =  sheet . formulas ?. some ( ( f : any )  =>  f . cell [ 0 ]  ===  r  &&  f . cell [ 1 ]  ===  c ) ; 
546-               
553+               const  hasFormula  =  sheet . formulas ?. some ( 
554+                 ( f : any )  =>  f . cell [ 0 ]  ===  r  &&  f . cell [ 1 ]  ===  c 
555+               ) ; 
556+ 
547557              if  ( hasFormula  &&  typeof  val  ===  'number' )  { 
548558                // Show computed value with indication it's calculated 
549559                cells . push ( `${ val }  ) ; 
@@ -583,35 +593,35 @@ function exportJson(doc: OSFDocument): string {
583593      case  'sheet' : { 
584594        const  sheet : any  =  {  ...block  } ; 
585595        delete  sheet . type ; 
586-          
596+ 
587597        if  ( sheet . data )  { 
588598          // Evaluate formulas and include computed values 
589599          const  evaluator  =  new  FormulaEvaluator ( sheet . data ,  sheet . formulas  ||  [ ] ) ; 
590-            
600+ 
591601          // Calculate dimensions including formula cells 
592602          const  dataCoords  =  Object . keys ( sheet . data ) . map ( ( k : string )  =>  k . split ( ',' ) . map ( Number ) ) ; 
593603          const  formulaCoords  =  ( sheet . formulas  ||  [ ] ) . map ( ( f : any )  =>  f . cell ) ; 
594604          const  allCoords  =  [ ...dataCoords ,  ...formulaCoords ] ; 
595-            
605+ 
596606          const  maxRow  =  Math . max ( ...allCoords . map ( c  =>  c [ 0 ] ! ) ) ; 
597607          const  maxCol  =  Math . max ( ...allCoords . map ( c  =>  c [ 1 ] ! ) ) ; 
598-            
608+ 
599609          // Get all computed values including formulas 
600610          const  allValues  =  evaluator . getAllComputedValues ( maxRow ,  maxCol ) ; 
601-            
611+ 
602612          // Convert to array format with computed values 
603613          sheet . data  =  Object . entries ( allValues ) . map ( ( [ cell ,  value ] )  =>  { 
604614            const  [ r ,  c ]  =  cell . split ( ',' ) . map ( Number ) ; 
605615            const  hasFormula  =  sheet . formulas ?. some ( ( f : any )  =>  f . cell [ 0 ]  ===  r  &&  f . cell [ 1 ]  ===  c ) ; 
606-             return  {   
607-               row : r ,   
608-               col : c ,   
616+             return  { 
617+               row : r , 
618+               col : c , 
609619              value, 
610-               computed : hasFormula   
620+               computed : hasFormula , 
611621            } ; 
612622          } ) ; 
613623        } 
614-          
624+ 
615625        if  ( sheet . formulas )  { 
616626          sheet . formulas  =  sheet . formulas . map ( ( f : any )  =>  ( { 
617627            row : f . cell [ 0 ] , 
@@ -763,9 +773,7 @@ function main(): void {
763773            output  =  renderXlsx ( doc ) ; 
764774            break ; 
765775          default :
766-             throw  new  Error ( 
767-               `Unknown format: ${ format }  
768-             ) ; 
776+             throw  new  Error ( `Unknown format: ${ format }  ) ; 
769777        } 
770778
771779        if  ( outputFile )  { 
0 commit comments