Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export results to word or excel #89

Closed
oerdem19 opened this issue Feb 21, 2023 · 4 comments
Closed

Export results to word or excel #89

oerdem19 opened this issue Feb 21, 2023 · 4 comments

Comments

@oerdem19
Copy link

What would you like gtools to add or change (and why)?
Option to Export results to excel or word.
Thank you.

@mcaceresb
Copy link
Owner

@oerdem19 This is not something that is within the purview of gtools. There are existing Stata tools to do this for various formats; if you have a specific use case I might be able to provide some guidance but I wouldn't add it to gtools directly.

@oerdem19
Copy link
Author

Thank you.

@mcaceresb
Copy link
Owner

@oerdem19 I didn't realize this was somewhat cumbersome; I don't use gstats like this but I figured I might as well code something to help. Please upgrade to the latest version 1.10.2 in the develop branch.

capture program drop _gstats_excel
program _gstats_excel
    syntax namelist(min=1 max=1) using/, [sheet(str) replace]
    if "`sheet'" == "" local sheet `namelist'
    mata _gstats_excel(`namelist', `"`using'"', `"`sheet'"', "`replace'" != "")
    * mata xlUsing = `"`using'"'
    * mata xlSheet = `"`sheet'"'
end

cap mata mata drop _gstats_excel()
mata
void function _gstats_excel(
    class GtoolsResults scalar GstatsOutput,
    string scalar xlUsing,
    string scalar xlSheet,
    real scalar replace)
{
    class xl scalar xlWb
    string vector xlSheets

    xlWb = xl()
    if ( fileexists(xlUsing) ) {
        xlWb.load_book(xlUsing)
        xlSheets = xlWb.get_sheets()
        if ( any(xlSheet :== xlSheets) & replace ) {
            xlWb.clear_sheet(xlSheet)
        }
        else if ( any(xlSheet :== xlSheets) ) {
            errprintf("'%s' already in workbook '%s' with no replace\n", xlSheet, xlUsing)
            _error(198)
        }
        else {
            xlWb.add_sheet(xlSheet)
        }
    }
    else {
        xlWb.create_book(xlUsing, xlSheet)
        xlSheets = xlWb.get_sheets()
    }

    xlWb.set_sheet(xlSheet)
    xlWb.put_string(1, 1, GstatsOutput.formatOutput())
    xlWb.close_book()
}
end

sysuse auto, clear
gstats tab price mpg, by(foreign rep78) mata
* mata GstatsOutput.formatOutput()
_gstats_excel GstatsOutput using gtools-excel.xlsx, replace

@oerdem19
Copy link
Author

Thank you very much. I think this can help many others too. Your program is very efficient and fast.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants