Encuentra Tablas de Consulta y Objetos Lista relacionados con Conexiones de Datos Externos
Contents
[
Hide
]
A veces, necesitas descubrir las Tablas de Consulta y Objetos Lista relacionados con alguna Conexión de Datos Externos. Las Tablas de Consulta están relacionadas con el objeto Conexión de Datos Externos con el Id de Conexión, mientras que los Objetos Lista están relacionados con una Tabla de Consulta.
Buscar Tablas de Consulta y Objetos de Lista relacionados con Conexiones de Datos Externos
Los siguientes códigos de ejemplo con archivo de Excel de muestra explican cómo encontrar Tablas y Objetos de Lista relacionados con la Conexión de Datos Externos.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from aspose.cells import CellsHelper, Workbook | |
from aspose.cells.tables import TableDataSourceType | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# The path to the documents directory. | |
dataDir = RunExamples.GetDataDir(".") | |
# Load workbook object | |
workbook = Workbook(dataDir + "sample.xlsm") | |
# Check all the connections inside the workbook | |
for i in range(len(workbook.data_connections)): | |
externalConnection = workbook.data_connections[i] | |
print("connection: " + externalConnection.name) | |
# Iterate all the worksheets | |
for j in range(len(workbook.worksheets)): | |
worksheet = workbook.worksheets[j] | |
# Check all the query tables in a worksheet | |
for k in range(len(worksheet.query_tables)): | |
qt = worksheet.query_tables[k] | |
# Check if query table is related to this external connection | |
if externalConnection.id == qt.connection_id and qt.connection_id >= 0: | |
# Print the query table name and print its refersto range | |
print("querytable " + qt.name) | |
n = qt.name | |
n = n.replace("+", "_") | |
n = n.replace("=", "_") | |
name = workbook.worksheets.names.get("'" + worksheet.name + "'!" + n) | |
if name != None: | |
range = name.get_range() | |
if range != None: | |
print("refersto: " + range.refers_to) | |
# Iterate all the list objects in this worksheet | |
for k in range(len(worksheet.list_objects)): | |
table = worksheet.list_objects[k] | |
# Check the data source type if it is query table | |
if table.data_source_type == TableDataSourceType.QUERY_TABLE: | |
# Access the query table related to list object | |
qt = table.query_table | |
# Check if query table is related to this external connection | |
if externalConnection.id == qt.connection_id and qt.connection_id >= 0: | |
# Print the query table name and print its refersto range | |
print("querytable " + qt.name) | |
print("Table " + table.display_name) | |
print("refersto: " + worksheet.name + "!" + CellsHelper.cell_index_to_name(table.start_row, table.start_column) + ":" + CellsHelper.cell_index_to_name(table.end_row, table.end_column)) | |
print() | |
print("Press any key to continue...") | |
input() |
La siguiente es la salida de consola al ejecutar los códigos de ejemplo anteriores con este archivo de Excel de muestra.
connection: AAPL Connection
querytable hp?s=AAPL+Historical+Prices
refersto: =Sheet1!$Q$1:$W$69
connection: BOSL066360W7_SQLEXPRESS Test
querytable BOSL066360W7_SQLEXPRESS Test
Table Table_BOSL066360W7_SQLEXPRESS_Test
refersto: Sheet1!A1:B3
connection: BOSL066360W7_SQLEXPRESS Test1
querytable BOSL066360W7_SQLEXPRESS Test_1
Table Table_BOSL066360W7_SQLEXPRESS_Test_1
refersto: Sheet1!D1:E2
connection: UWTI Connection
querytable hp?s=UWTI+Historical+Prices
refersto: =Sheet1!$H$1:$N$69