Shapes

更新时间:
复制 MD 格式

This topic describes how to obtain the number of shapes, add a chart, and set the chart title when you use a table document.

Obtain shape objects

Obtains all shape objects on the current worksheet.

  • Syntax

    Expression.ActiveWorkbook.ActiveSheet.Shapes

    Expression: the document type application object.

  • Example

    async function example() {
      await instance.ready();
    
      const app = instance.Application;
      
      // Obtain the active worksheet in the active workbook.
      const activeSheet = await app.ActiveWorkbook.ActiveSheet;
    
      // Obtain all shape objects on the current worksheet.
      const shapes = await activeSheet.Shapes;
    }

View the number of shapes

View the number of shapes on the current worksheet.

  • Syntax

    Expression.ActiveWorkbook.ActiveSheet.Shapes.Count

    Expression: the document type application object.

  • Example

    async function example() {
      await instance.ready();
    
      const app = instance.Application;
      
      // Obtain the active worksheet in the active workbook.
      const activeSheet = await app.ActiveWorkbook.ActiveSheet;
    
      // Obtain the shape objects on the current worksheet.
      const shapes = await activeSheet.Shapes;
    
      // View the number of shapes.
      const count = await shapes.Count;
      console.log(count);
    }

Add a chart

Add a chart to the current worksheet.

  • Syntax

    Expression.ActiveWorkbook.ActiveSheet.Shapes.AddChart2({ Style, XlChartType, Left, Top, Width, Height })

    Expression: the document type application object.

  • Parameters

    Parameter

    Type

    Required

    Description

    Style

    String

    No

    Specifies the style of the new chart.

    XlChartType

    Enum

    No

    Specifies the type of the new chart.

    Left

    Number

    No

    Specifies the left margin of the chart. Unit: pixels.

    Top

    Number

    No

    Specifies the top margin of the new chart. Unit: pixels.

    Width

    Number

    No

    Specifies the width of the new chart. Unit: pixels.

    Height

    Number

    No

    Specifies the height of the new chart. Unit: pixels.

  • Example

    async function example() {
      await instance.ready();
    
      const app = instance.Application;
      
      // Obtain the active worksheet in the active workbook.
      const activeSheet = await app.ActiveWorkbook.ActiveSheet;
    
      // Obtain all shape objects on the current worksheet.
      const shapes = await activeSheet.Shapes;
    
      // Add a 300 x 300 rectangle.
      await shapes.AddChart2(340, 51, 0, 0, 300, 300);
    }

Operations on a single shape

Obtain a single shape object

Obtain an object in the drawing layer. For example, the object can be a custom shape, a polygon, an OLE object, or an image.

  • Syntax

    Expression.ActiveWorkbook.ActiveSheet.Shapes.Item(Index)

    Expression: the document type application object.

  • Example

    async function example() {
      await instance.ready();
    
      const app = instance.Application;
      
      // Obtain the active worksheet in the active workbook.
      const activeSheet = await app.ActiveWorkbook.ActiveSheet;
    
      // Obtain all shape objects on the current worksheet.
      const shapes = await activeSheet.Shapes;
    
      // Add a 300 x 300 rectangle.
      await shapes.AddChart2(340, 51, 0, 0, 300, 300);
    
      // Obtain a single shape object.
      const shape = await shapes.Item(1);
    }

Set the title of a single shape

  • Syntax

    Expression.ActiveWorkbook.ActiveSheet.Shapes.Item(Index).Title

    Expression: the document type application object.

  • Example

    async function example() {
      await instance.ready();
    
      const app = instance.Application;
      
      // Obtain the active worksheet in the active workbook.
      const activeSheet = await app.ActiveWorkbook.ActiveSheet;
    
      // Obtain all shape objects on the current worksheet.
      const shapes = await activeSheet.Shapes;
    
      // Add a 300 x 300 rectangle.
      await shapes.AddChart2(340, 51, 0, 0, 300, 300);
    
      // Obtain a single shape object.
      const shape = await shapes.Item(1);
    
      // Set the title of the shape.
      shape.Title = 'WebOffice';
    }

Obtain the name of a single shape

  • Syntax

    Expression.ActiveWorkbook.ActiveSheet.Shapes.Item(Index).Name

    Expression: the document type application object.

  • Example

    //@file=base.xlsx
    async function example() {
      await instance.ready();
    
      const app = instance.Application;
      
      // Obtain the active worksheet in the active workbook.
      const activeSheet = await app.ActiveWorkbook.ActiveSheet;
    
      // Obtain all shape objects on the current worksheet.
      const shapes = await activeSheet.Shapes;
    
      // Add a 300 x 300 rectangle.
      const shape = await shapes.AddChart2(340, 51, 0, 0, 300, 300);
    
      // Obtain the name of the shape.
      const name = await shape.Name;
      console.log(name);
    }

Obtain the ID of a single shape

  • Syntax

    Expression.ActiveWorkbook.ActiveSheet.Shapes.Item(Index).ID

    Expression: the document type application object.

  • Example

    //@file=base.xlsx
    async function example() {
      await instance.ready();
    
      const app = instance.Application;
      
      // Obtain the active worksheet in the active workbook.
      const activeSheet = await app.ActiveWorkbook.ActiveSheet;
    
      // Obtain all shape objects on the current worksheet.
      const shapes = await activeSheet.Shapes;
    
      // Add a 300 x 300 rectangle.
      const shape = await shapes.AddChart2(340, 51, 0, 0, 300, 300);
    
      // Obtain the ID of the shape.
      const id = await shape.ID;
      console.log(id);
    }

Select a single shape

  • Syntax

    Expression.ActiveWorkbook.ActiveSheet.Shapes.Item(Index).Select()

    Expression: the document type application object.

  • Example

    async function example() {
      await instance.ready();
    
      const app = instance.Application;
      
      // Obtain the active worksheet in the active workbook.
      const activeSheet = await app.ActiveWorkbook.ActiveSheet;
    
      // Obtain all shape objects on the current worksheet.
      const shapes = await activeSheet.Shapes;
    
      // Obtain a single shape object.
      const shape = await shapes.AddChart2(340, 51, 0, 0, 300, 300);
    
      // Obtain the data source of the shape.
      const source = await activeSheet.Range('A1:B4');
    
      // Set the data source of the shape.
      await shape.Chart.SetSourceData(source, 1);
    
      // Select a single shape.
      setTimeout( async () => {
        await shape.Select();
      }, 3000);
    }

Delete a single shape

  • Syntax

    Expression.ActiveWorkbook.ActiveSheet.Shapes.Item(Index).Delete()

    Expression: the document type application object.

  • Example

    async function example() {
      await instance.ready();
    
      const app = instance.Application;
      
      // Obtain the active worksheet in the active workbook.
      const activeSheet = await app.ActiveWorkbook.ActiveSheet;
    
      // Obtain all shape objects on the current worksheet.
      const shapes = await activeSheet.Shapes;
    
      // Obtain a single shape object.
      const shape = await shapes.AddChart2(340, 51, 0, 0, 300, 300);
    
      // Obtain the data source of the shape.
      const source = await activeSheet.Range('A1:B4');
    
      // Set the data source of the shape.
      await shape.Chart.SetSourceData(source, 1);
    
      // Delete a single shape.
      setTimeout( async () => {
        await shape.Delete();
      }, 3000);
    }

Operations on a single chart

Obtain a single chart object

  • Syntax

    Expression.ActiveWorkbook.ActiveSheet.Shapes.Item(Index).Chart

    Expression: the document type application object.

  • Example

    async function example() {
      await instance.ready();
    
      const app = instance.Application;
      
      // Obtain the active worksheet in the active workbook.
      const activeSheet = await app.ActiveWorkbook.ActiveSheet;
    
      // Obtain all shape objects on the current worksheet.
      const shapes = await activeSheet.Shapes;
    
      // Obtain a single shape object.
      const shape = await shapes.AddChart2(340, 51, 0, 0, 300, 300);
    
      // Obtain a single chart object.
      const chart = await shape.Chart;
    }

Set the source data of a chart

Set the source data range for the specified chart.

  • Syntax

    Expression.ActiveWorkbook.ActiveSheet.Shapes.Item(Index).Chart.SetSourceData({ Source, PlotBy })

    Expression: the document type application object.

  • Parameters

    Parameter

    Type

    Required

    Description

    Source

    Range

    Yes

    Specifies the range that contains the source data. You can use the Range object to specify the data source range.

    PlotBy

    Enum

    No

    Specifies the chart type.

  • Example

    async function example() {
      await instance.ready();
    
      const app = instance.Application;
      
      // Obtain the active worksheet in the active workbook.
      const activeSheet = await app.ActiveWorkbook.ActiveSheet;
    
      // Obtain all shape objects on the current worksheet.
      const shapes = await activeSheet.Shapes;
    
      // Add a single shape object.
      const shape = await shapes.AddChart2(340, 51, 0, 0, 300, 300);
    
      // Obtain a single chart object.
      const chart = await shape.Chart;
    
      // Obtain the source data of the chart.
      const source = await activeSheet.Range('A1:D4');
    
      // Set the source data for the chart.
      await chart.SetSourceData(source, 1);
    }

Set title visibility

If the axis or chart has a visible title, set the value to true.

  • Syntax

    Expression.ActiveWorkbook.ActiveSheet.Shapes.Item(Index).Chart.HasTitle

    Expression: the document type application object.

  • Example

    async function example() {
      await instance.ready();
    
      const app = instance.Application;
      
      // Obtain the active worksheet in the active workbook.
      const activeSheet = await app.ActiveWorkbook.ActiveSheet;
    
      // Obtain all shape objects on the current worksheet.
      const shapes = await activeSheet.Shapes;
    
      // Obtain a single shape object.
      const shape = await shapes.AddChart2(340, 51, 0, 0, 300, 300);
    
      // Obtain a single chart object.
      const chart = await shape.Chart;
    
      // Obtain the source data of the chart.
      const source = await activeSheet.Range('A1:D4');
    
      // Set the source data for the chart.
      await chart.SetSourceData(source, 1);
    
      // Set the title to be invisible.
      chart.HasTitle = false;
    }

Set legend visibility

If the chart has a legend, set the value to true.

  • Syntax

    Expression.ActiveWorkbook.ActiveSheet.Shapes.Item(Index).Chart.HasLegend

    Expression: the document type application object.

  • Example

    async function example() {
      await instance.ready();
    
      const app = instance.Application;
      
      // Obtain the active worksheet in the active workbook.
      const activeSheet = await app.ActiveWorkbook.ActiveSheet;
    
      // Obtain all shape objects on the current worksheet.
      const shapes = await activeSheet.Shapes;
    
      // Obtain a single shape object.
      const shape = await shapes.AddChart2(340, 51, 0, 0, 300, 300);
    
      // Obtain a single chart object.
      const chart = await shape.Chart;
    
      // Obtain the source data of the chart.
      const source = await activeSheet.Range('A1:D4');
    
      // Set the source data for the chart.
      await chart.SetSourceData(source, 1);
    
      // Set the legend to be invisible.
      chart.HasLegend = false;
    }

Set the title

  • Syntax

    Expression. ActiveWorkbook.ActiveSheet.Shapes.Item(Index).Chart.ChartTitle.Text

    Expression: the document type application object.

  • Example

    async function example() {
      await instance.ready();
    
      const app = instance.Application;
      
      // Obtain the active worksheet in the active workbook.
      const activeSheet = await app.ActiveWorkbook.ActiveSheet;
    
      // Obtain all shape objects on the current worksheet.
      const shapes = await activeSheet.Shapes;
    
      // Obtain a single shape object.
      const shape = await shapes.AddChart2(340, 51, 0, 0, 300, 300);
    
      // Obtain a single chart object.
      const chart = await shape.Chart;
    
      // Obtain the source data of the chart.
      const source = await activeSheet.Range('A1:D4');
    
      // Set the source data for the chart.
      await chart.SetSourceData(source, 1);
    
      // Set a new title.
      chart.ChartTitle.Text ='This is the new title';
    }