2

In LINQPad software, I would like to know if it is possible to save or export the Results in an Html file.

The result tab is

enter image description here

I would like to save the result and be able to send it by email.

I know I can do it by using some extarnal instructions like

string htmlResult = Util.Run ("test.linq", QueryResultFormat.Html).AsString();

or

lprun -format=html foo.linq > output.html

But I would like to be able to export the Results on demand, not always. I would like to bring a minimum of interactivity to my scripts. I would like to let the user of the script decide when to continue and when to send the script.

I just need an access to the body element of the Results. LINQpad made me crazy about this because I can generate the Html with Util.Run or lprun, I can also access the head of this Result by using Util.HtmlHead, I can clear the Result by using Util.ClearResults(), I can write everything I want in this Result page BUT impossible to retrieve it. I can export it manually by using the Export button on top right but I cannot do it by programmation.

I created a workaround by using the extensions with a new Util class

public static class Util2
{
    // https://www.linqpad.net/CustomizingDump.aspx
    // http://www.linqpad.net/lprun.aspx
    // https://stackoverflow.com/questions/3555317/linqpad-extension-methods
    private static StringBuilder htmlStringBuilder = new StringBuilder();

    /// <summary>Writes to LINQPad result window and save the result in html file</summary>
    public static object Save(this object o, bool writeFile = false, string fileName = null)
    {
        htmlStringBuilder.Append(Util.ToHtmlString(o));
        return o;
    }

    public static string GetResults()
    {
        return htmlStringBuilder.ToString();
    }

    public static void ClearResults()
    {
        htmlStringBuilder.Clear();
    }
}

I can use this by simply adding a .Save() after all my .Dump(). I cannot use the Console.Write() anymore and try to use Util.RawHtml() a maximum.

Bastien Vandamme
  • 17,659
  • 30
  • 118
  • 200

1 Answers1

2

Right now, there is no way to export the contents of the results window programmatically. But you can get fairly close by extracting the raw HTML:

string rawHTML = Util.InvokeScript (true, "eval", "document.documentElement.outerHTML");

This is different to the "cooked" HTML that you get from the interactive "Export to HTML" option. The cooked HTML is cleansed of hyperlinks and scripts that won't work without the LINQPad host.

There was a LINQPad feature request some time back to add a method such as Util.GetResultsAsHtml() to extract the exported HTML. The difficulty with adding such a method is that it would fail when a script was called via lprun (because the results are sent to Console.Out instead of a browser).

Joe Albahari
  • 30,118
  • 7
  • 80
  • 91