Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Splitting a mata functin into parts

    Hi,

    I am very much a novice using mata, and i am trying to write a small piece of mata to change rowheights of an excel file.

    My final command looks like this:

    Code:
    excelrowht, file("mytestexport.xlsx") sheet( "Sheet1") linehtlist("1 / 10  " "11/20 @35" "21,30 @50" "31/40 @65" )
    I have a chunk of Stata code to parse
    It consists of a small bit of .ado and small piece of .mata

    However, the mata is really inefficent as it open and closes everything each time.
    Ideally i would like function to open the workbook, another to make edits, and a final one to close it.
    But alas, i have no idea how to split the function apart and still allow it to work.

    Code:
    cap prog drop excelrowht
    prog define excelrowht
    syntax, linehtlist(string asis) file(string asis) sheet(string asis)
    
    foreach group in `linehtlist' {
    
    
    if strpos("`group'" ,",")>0 {
        tokenize "`group'" , parse("@," )
            local ls `1'
                local le `3'
                    local ht `5'
            di "`ls' `le' `ht'"
    mata: excelrowht(`file', `sheet', `ls' , `ht', `le')        
            } // end if
    else {
        tokenize "`group'" , parse("@")
            local linelist `1'
                local ht `3'
    
        foreach ls of numlist `linelist' {
            di "`ls' `ht'"
            
    mata: excelrowht(`file', `sheet', `ls' , `ht')
            
                } // end foreach
     } // end else
     } // end group
    end
    My Mata code,

    Code:
    mata
    
            void excelrowht(    string    scalar file        , ///
                                string     scalar sheet    , ///
                                real     vector rowstart , ///
                            |    real     scalar rowht     , ///
                                real     scalar rowend)
     {
     
    
    if (rowht==.) rowht =30
    if (rowend==.) rowend=rowstart    
    
    printf("excelrowht( %s, %s, %f , %f,  %f )",file,sheet,rowstart,rowend,rowht)
    
    
    class xl scalar b
     b =xl()
      b.load_book(file)
      b.set_mode("open")    
      b.set_sheet(sheet)
      b.set_keep_cell_format("on")
     
    for (i=1; i<=length(rowstart); i++) {  
        b.set_row_height(rowstart[i],rowend[i],rowht)
                }
    b.close_book()
    
     }
    end
    My attempt it splitting it apart into three functions

    Code:
    mata
            void excelrowhtopen(    string    scalar file        , ///
                                string     scalar sheet     ///
                                )
     {
    printf("excelrowht( %s, %s, )",file,sheet)
    
    
    class xl scalar b
     b =xl()
      b.load_book(file)
      b.set_mode("open")    
      b.set_sheet(sheet)
        }
        
    
        void excelrowhtput(        real     scalar rowstart , ///
                            |    real     scalar rowht     , ///
                                real     scalar rowend)
     {
     class xl scalar b
     b =xl()
    if (rowht==.) rowht =30
    if (rowend==.) rowend=rowstart    
    
    printf("excelrowht( %f , %f,  %f )",rowstart,rowend,rowht)
     
    for (i=1; i<=length(rowstart); i++) {  
        b.set_row_height(rowstart[i],rowend[i],rowht)
                }
     }
    void excelrowhtclose( )
     {
     b.close_book()
     }
    end
Working...
X