Announcement

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

  • Hyperlink to the file generated/modified by putexcel

    This is a very minor request/question. Several of the user-made commands I use (e.g. estout, and iebaltab) have a nifty feature where they provide a hyperlink to the file that they write so that you can just click from the results window rather than browsing through your files. Is there any way to get putexcel to do this as well? I've been searching about but can't find much information about how this works. Thanks!
    Last edited by Jonathan Seiden; 27 Dec 2018, 08:38.

  • #2
    Glad you asked this, as I've wanted to do things like this, too. The more generic version of this request, though, is "How do I display a highlighted piece of text in the results window such that, if a user clicks on that text, some command is executed in the command window?" That's the thing for which I have not figured out how to seach in Stata's documentation.

    Note that, at least under Windows, entering in the command window !"MyFileRef" will cause Windows to open that file in its default application. So, entering !"My Excel File.xls" will, under Windows, shell out to the OS and open that file in Excel.

    Comment


    • #3
      Aha! Found the answer by browsing in a program that implemented this feature, and with substantial difficulty, was able to find what was to me some obscure documentation on how to do this. See p. 602 in [P], most easily found with -help smcl-. This is one of those "If you already know how to do this, this documentation would be helpful" sections of the Stata manuals.

      Code:
      // Two examples
      di as smcl  "Click to open the auto file: "  `"{stata sysuse auto, clear}"'
      di as smcl "Click to open an Excel file: " `"{stata !myfile.xls}"'

      Comment


      • #4
        The following empirical research
        Code:
        log using gnxl, replace smcl
        estout using foo, replace
        log close
        led me to learn that the following text from my results window, complete with the link on "foo" in the final line
        Code:
        . estout using foo, replace
        (note: file foo not found)
        (output written to foo)
        is rendered in SMCL as
        Code:
        {com}. estout using foo, replace
        {res}{txt}(note: file foo not found)
        (output written to {browse  `"foo"'})
        so it appears the browse SMCL directive is what does the trick. Now admittedly the output of help smcl tells us this directive

        displays text as a link that will launch the user's browser
        and suggests that the argument will likely be a URL, so perhaps this is undocumented behavior, or else "the user's browser" is something other that what I might guess it to be.

        Comment


        • #5
          Thanks so much Mike and William! Great responses and awesome to learn about SMCL!

          As I am new to this, and just want to add a convenience line in my .do files, I settled on the following solution that combined both of your replies.

          Code:
          sysuse auto, clear
          sum price
          putexcel set "example.xlsx", replace
          putexcel A1 = "`r(N)'" B1 = "`r(mean)'"
          di as smcl "output written to {browse  "`"example.xlsx}"'"
          Last edited by Jonathan Seiden; 07 Jan 2019, 14:39. Reason: Added combined solution

          Comment


          • #6
            Subsequent exploration reported at

            https://www.statalist.org/forums/for...ul-with-esttab

            has shown that the SMCL browse directive will not open filenames containing embedded spaces for every web browser. It specifically fails under macOS using Safari 12.1.1, and I would expect most other versions of Safari as well.

            Because the SMCL browse directive is designed to pass a URL to a browser, it encodes spaces as an escape sequence, but Safari does not decode the escape sequences before attempting to open the file.

            Comment

            Working...
            X