*! version 1.3 26Aug2022 Mead Over hack of Lars Kroll's -grexport- program gph2xl , version 15.1 syntax [name(name=gphname)] [using/], /// [ Wide norestore saving(string asis) List replace modify /// excel(string asis) KEEPsets(numlist) /// nopng noemf] // Error Messages if "`using'"~="" & "`gphname'"~="" { di as err "Specify either -gphname- or -using gphname.gph-, not both." exit 198 } if "`using'"=="" & "`gphname'"=="" { local gphname "Graph " } else { if "`gphname'"~="" { graph display `gphname' } if "`using'" ~= "" { serset clear graph use "`using'" } } if (`"`list'"'=="")&(`"`saving'"'=="")&(`"`excel'"'=="")&("`restore'"!="norestore") { disp as error "You must specify at least one of the four options:" /// _n "list, saving(), excel() and norestore." /// _n(2) " - If you specify list, then the values are listed." /// _n " - If you specify saving(), then values are exported to a new data set." /// _n " - If you specify norestore, then the new data set is created in the memory." /// _n(2) "For more details, see {help gph2xl:on-line help for gph2xl}." error 498 } if ("`replace'"!="") & (`"`saving'"'==""&`"`excel'"'=="") { disp as error "Option -replace- is senseless without either the saving() or the excel() option." /// _n "For more details, see {help gph2xl:on-line help for gph2xl}." error 498 } // Initialize if "`excel'"~="" { tempname picture /* The -wmf- and -pdf- image options are omitted because: -the -wmf- image format is superseded by the -emf- format -in Version 15, Stata's -putexcel, picture(picture_file)- command does not support the PDF format. See -help putexcel, picture- */ * Copy PNG file into the Excel file on the -png_image- worksheet if "`png'"~="nopng" { qui gr export `picture'.png, replace putexcel set `"`excel'"', `replace' `modify' sheet(png_image) putexcel A1 = picture(`picture'.png) erase `picture'.png local modify modify local replace } * Copy EMF file into the Excel file on the EMF_image worksheet if "`emf'"~="noemf" { qui gr export `picture'.emf, replace putexcel set `"`excel'"', `replace' `modify' sheet(emf_image) putexcel A1 = picture(`picture'.emf) erase `picture'.emf local modify modify local replace } } * Default is to preserve data in memory to be automatically restored * at program termination. Option -norestore- causes -gph2xl- to destroy * the data in memory and replace them with the -serset- data. if "`restore'" ~= "norestore" { preserve } drop _all tempfile gph2xl // Collect information on the graph currently in use if "`keepsets'"=="" { capt local setn : serset id if _rc~=0 { di as err "No graph data is currently in memory." /// _n "Either display a graph before executing -gph2xl-" /// _n "or call a graph from disc using the syntax:" /// _n "gph2xl using filename.gph, list" exit 198 } local keepsets 0(1)`setn' } foreach set of numlist `keepsets' { capture serset set `set' if _rc == 0 { serset use , clear qui des , varlist foreach v of varlist `r(varlist)' { rename `v' v_`v' } gen set = `set' if `set' > 0 { quietly capture append using "`gph2xl'" } quietly save `gph2xl' , replace } } // Options if "`wide'" != "" { quietly by set , sort : gen nobs = _n quietly ds set nobs , not quietly reshape wide "`r(varlist)'" , i(nobs) j(set) } if "`list'" !="" { list , noobs } if "`saving'" != "" { qui compress save "`saving'" , `replace' } if "`excel'" != "" { putexcel set `"`excel'"', `replace' `modify' sheet(data) qui compress mkmat _all, matrix(data) obs putexcel A1 = matrix(data), names putexcel clear } end * 1.0.0 23Aug2006 @ 13:37:36 by LEK - search -grexport- * 1.0.1 8Apr2019 Hacked by Mead Over * Allow the user to specify a graph name by any of: * o with -gph2xl , ... - (for the graph currently occupying the Graph window) * o with -gph2xl gphname- * o with -gph2xl using gphname.gph- * Only the -gph2xl using- syntax clears previous serset's. Otherwise user must do so. * -excel- option: Use -putexcel- to save the image and the variables into an Excel file * -keepsets(numlist)- option to designate the sersets to keep * Note that the graphing options -xscale(log)- and -yscale(log)- do not change the serset data * 1.0.2 9Apr2019 Fix bug in option -wide- * 1.0.3 9Apr2019 Add EMF file to Excel file (comment out the png & wmf file types) * 1.2 22Apr2019 Add back PNG. Add the nopdf and noemf options. * Tried to add a PDF option, but in Stata version 15, -putexcel, picture(filename)- * does not support PDF pictures. EMF is vector plus raster. PNG is raster only. * For the -using- syntax, the -nodraw- option prevents exporting the graph, * e.g. to png or emf formats. * 1.3 26Aug2022 Bug fix. In line 77 corrected comparison to i f "`restore'" ~= "norestore" -