+ Reply to Thread
Results 1 to 5 of 5

Chart to worksheet programmatically -- coordinates?

  1. #1
    Petr Prikryl
    Guest

    Chart to worksheet programmatically -- coordinates?

    Hi,

    I am exporting the Excel document and my goal is to
    place several chart objects into the worksheet.
    I know the range (rectangle) of cells where the chart
    should be placed. How can I convert the cell coordinates
    into points that are used in the ChartObjects' Add() method?

    Details:

    The MSDN Library contains the example with title
    "Code: Add a Chart to a Worksheet Programmatically (C#)"

    Excel.Worksheet thisWorksheet;
    thisWorksheet = thisWorkbook.ActiveSheet as Excel.Worksheet;
    Excel.ChartObjects charts =
    (Excel.ChartObjects)thisWorksheet.ChartObjects(Type.Missing);

    // Adds a chart at x = 100, y = 300, 500 points wide and 300 tall.
    Excel.ChartObject chartObj = charts.Add(100, 300, 500, 300);
    Excel.Chart chart = chartObj.Chart;

    Instead of fixed values 100, 300, 500, 300 -- how can I get the coordinates
    where visually the cells from range (say) "H4:J20" sit?

    Thanks for your time and experience,
    Petr
    --
    Petr Prikryl (prikrylp at skil dot cz)



  2. #2
    K Dales
    Guest

    RE: Chart to worksheet programmatically -- coordinates?

    A Range object has Top, Left, Width and Height properties that correspond
    directly to the same properties in the ChartObject. So (assuming you have
    set the variable ChartRange to equal the range you are interested in filling):
    Excel.ChartObject chartObj = charts.Add(ChartRange.Left, ChartRange.Top,
    ChartRange.Width, ChartRange.Height);
    Hope the syntax is OK; I don't work with C# - but I am sure you will get the
    idea.

    "Petr Prikryl" wrote:

    > Hi,
    >
    > I am exporting the Excel document and my goal is to
    > place several chart objects into the worksheet.
    > I know the range (rectangle) of cells where the chart
    > should be placed. How can I convert the cell coordinates
    > into points that are used in the ChartObjects' Add() method?
    >
    > Details:
    >
    > The MSDN Library contains the example with title
    > "Code: Add a Chart to a Worksheet Programmatically (C#)"
    >
    > Excel.Worksheet thisWorksheet;
    > thisWorksheet = thisWorkbook.ActiveSheet as Excel.Worksheet;
    > Excel.ChartObjects charts =
    > (Excel.ChartObjects)thisWorksheet.ChartObjects(Type.Missing);
    >
    > // Adds a chart at x = 100, y = 300, 500 points wide and 300 tall.
    > Excel.ChartObject chartObj = charts.Add(100, 300, 500, 300);
    > Excel.Chart chart = chartObj.Chart;
    >
    > Instead of fixed values 100, 300, 500, 300 -- how can I get the coordinates
    > where visually the cells from range (say) "H4:J20" sit?
    >
    > Thanks for your time and experience,
    > Petr
    > --
    > Petr Prikryl (prikrylp at skil dot cz)
    >
    >
    >


  3. #3
    Tom Ogilvy
    Guest

    Re: Chart to worksheet programmatically -- coordinates?

    Cells have Top, Left, Height, Width properties.

    --
    Regards,
    Tom Ogilvy

    "Petr Prikryl" <prikrylp at skil dot cz> wrote in message
    news:%[email protected]...
    > Hi,
    >
    > I am exporting the Excel document and my goal is to
    > place several chart objects into the worksheet.
    > I know the range (rectangle) of cells where the chart
    > should be placed. How can I convert the cell coordinates
    > into points that are used in the ChartObjects' Add() method?
    >
    > Details:
    >
    > The MSDN Library contains the example with title
    > "Code: Add a Chart to a Worksheet Programmatically (C#)"
    >
    > Excel.Worksheet thisWorksheet;
    > thisWorksheet = thisWorkbook.ActiveSheet as Excel.Worksheet;
    > Excel.ChartObjects charts =
    > (Excel.ChartObjects)thisWorksheet.ChartObjects(Type.Missing);
    >
    > // Adds a chart at x = 100, y = 300, 500 points wide and 300 tall.
    > Excel.ChartObject chartObj = charts.Add(100, 300, 500, 300);
    > Excel.Chart chart = chartObj.Chart;
    >
    > Instead of fixed values 100, 300, 500, 300 -- how can I get the

    coordinates
    > where visually the cells from range (say) "H4:J20" sit?
    >
    > Thanks for your time and experience,
    > Petr
    > --
    > Petr Prikryl (prikrylp at skil dot cz)
    >
    >




  4. #4
    Tom Ogilvy
    Guest

    Re: Chart to worksheet programmatically -- coordinates?

    So this is how it would be done in VBA,

    set r = Range("H4:J20")
    activesheet.chartobjects.add r.left, r.top, _
    r.width, r.height

    I am sure you can adapt that to your situation.

    --
    Regards,
    Tom Ogilvy

    "Tom Ogilvy" <[email protected]> wrote in message
    news:[email protected]...
    > Cells have Top, Left, Height, Width properties.
    >
    > --
    > Regards,
    > Tom Ogilvy
    >
    > "Petr Prikryl" <prikrylp at skil dot cz> wrote in message
    > news:%[email protected]...
    > > Hi,
    > >
    > > I am exporting the Excel document and my goal is to
    > > place several chart objects into the worksheet.
    > > I know the range (rectangle) of cells where the chart
    > > should be placed. How can I convert the cell coordinates
    > > into points that are used in the ChartObjects' Add() method?
    > >
    > > Details:
    > >
    > > The MSDN Library contains the example with title
    > > "Code: Add a Chart to a Worksheet Programmatically (C#)"
    > >
    > > Excel.Worksheet thisWorksheet;
    > > thisWorksheet = thisWorkbook.ActiveSheet as Excel.Worksheet;
    > > Excel.ChartObjects charts =
    > > (Excel.ChartObjects)thisWorksheet.ChartObjects(Type.Missing);
    > >
    > > // Adds a chart at x = 100, y = 300, 500 points wide and 300 tall.
    > > Excel.ChartObject chartObj = charts.Add(100, 300, 500, 300);
    > > Excel.Chart chart = chartObj.Chart;
    > >
    > > Instead of fixed values 100, 300, 500, 300 -- how can I get the

    > coordinates
    > > where visually the cells from range (say) "H4:J20" sit?
    > >
    > > Thanks for your time and experience,
    > > Petr
    > > --
    > > Petr Prikryl (prikrylp at skil dot cz)
    > >
    > >

    >
    >




  5. #5
    Petr Prikryl
    Guest

    Re: Chart to worksheet programmatically -- coordinates?

    > "Petr Prikryl" wrote:
    > > [...] how can I get the coordinates
    > > where visually the cells from range (say) "H4:J20" sit?


    "K Dales" wrote...
    > A Range object has Top, Left, Width and Height properties that correspond
    > directly to the same properties in the ChartObject. So (assuming you have
    > set the variable ChartRange to equal the range you are interested in

    filling):
    > Excel.ChartObject chartObj = charts.Add(ChartRange.Left, ChartRange.Top,
    > ChartRange.Width, ChartRange.Height);
    > [...]


    Thanks, it works fine!
    Thanks also to Tom Ogilvy who answered similarly.

    Petr
    --
    Petr Prikryl (prikrylp at skil dot cz)



+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1