I’m working on uploading data to Excel 2003 tables and charts. There are a dozen charts on the sheet and I need to configure them one by one.

At the moment I am getting an Excel chart by its ordinal number ( aId ) like this:

 sheet := Excel.ActiveWorkBook.Sheets[aSheetName]; // Выбираем лист sheet.ChartObjects[aId].Activate; // Активируем aChartId диаграмму chart := Excel.ActiveWorkBook.ActiveChart; // Получаем диаграмму для работы 

The problem is that the order / sequence of diagrams is clearly not visible in Excel, and when inserting new diagrams, you have to cut / paste them all in order for their indices to go in order (for example, from top to bottom).

The question is how to get a chart knowing the cell above which it is located? (assuming that there is only 1 diagram above the cell). Something like chart := sheet.ChartObjects.FindByCell(x, y);

    1 answer 1

    At the moment, the working solution is to go through all the diagrams and check if the cell falls into their area:

     // Диаграммы идут от 1 до Count for I := 1 to sheet.ChartObjects.Count do begin // Сохраняем область диаграммы в прямоугольник `r` r := TXLRange.New( TXLCell.New(sheet.ChartObjects[I].TopLeftCell.Address), TXLCell.New(sheet.ChartObjects[I].BottomRightCell.Address)); // Проверяем ячейку на вхождение в прямоугольник if r.Contains(aChartCell) then begin // Получаем искомую диаграмму sheet.ChartObjects[I].Activate; chart := aExcel.ActiveWorkBook.ActiveChart; end; end; 

    PS TXLRange and TXLCell are primitive structures for storing a rectangle and a cell.