You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Iron Python脚本按限制条件导出Excel数据失效问题求助

Fixing IronPython Script to Export Only Rows Where Sales > 200 in Spotfire

Let's fix that script for you! The core issue with your current code is that you're trying to compare an entire column object to a numeric value, which doesn't actually filter rows—instead, it's an invalid comparison that either skips your export logic entirely or ends up exporting all rows because you're using the full unfiltered row set. Here's how to adjust it to properly export only rows where Sales > 200:

Corrected Script

from Spotfire.Dxp.Data.Export import DataWriterTypeIdentifiers
from System.IO import File, Directory
from System.Collections.Generic import List
from Spotfire.Dxp.Data import DataValueCursor, IndexSet

# Define output directory and file path (use raw strings to avoid escape character issues)
output_dir = r"C:\Users\47586\Documents\Daily activities\POC"
output_file = r"{0}\Filename.xls".format(output_dir)

# Create directory if it doesn't exist
if not Directory.Exists(output_dir):
    Directory.CreateDirectory(output_dir)

# Get the target data table
target_table = Document.Data.Tables["Test"]

# Define which columns to export
columns_to_export = List[str]()
columns_to_export.Add("Country_Name")
columns_to_export.Add("Sales")

# Create a cursor to safely access values in the Sales column
sales_cursor = DataValueCursor.CreateNumeric(target_table.Columns["Sales"])

# Initialize an IndexSet to track rows that meet our condition (starts with all rows unselected)
filtered_rows = IndexSet(target_table.RowCount, False)

# Iterate through each row and check the Sales value
for row in target_table.GetRows(sales_cursor):
    # Skip rows with null Sales values, then check if value is greater than 200
    if sales_cursor.CurrentValue is not None and sales_cursor.CurrentValue > 200:
        filtered_rows.Add(row.Index)

# Export only the filtered rows to Excel
writer = Document.Data.CreateDataWriter(DataWriterTypeIdentifiers.ExcelXlsDataWriter)
stream = File.OpenWrite(output_file)
try:
    writer.Write(stream, target_table, filtered_rows, columns_to_export)
finally:
    # Ensure the file stream is always closed/disposed, even if an error occurs
    stream.Close()
    stream.Dispose()

Key Changes Explained

  • Removed invalid column comparison: Your original if Document.Data.Tables["Test"].Columns["Sales"] > 200 line doesn't work because you're comparing a column object to a number, not checking individual row values. We replaced this with a proper row-by-row check using a DataValueCursor (Spotfire's recommended way to access row data in IronPython).
  • Filtered row selection: Instead of using AllRows.GetSelection() (which grabs all rows by default), we create an IndexSet and only add rows where Sales > 200 to it. This tells the exporter exactly which rows to include.
  • Safer path handling: Using raw strings (r"") for Windows paths prevents issues with escape characters (like \U or \D being misinterpreted).
  • Resource cleanup: Added a try/finally block to make sure the file stream is always closed properly, even if the export hits an error.

Additional Notes

  • If your Sales column is stored as a string (unlikely, but possible), you'll need to convert it to a numeric type first (e.g., float(sales_cursor.CurrentValue) > 200).
  • If you ever want to export rows based on Spotfire's active visual filters instead of a hardcoded condition, you can replace the filtered IndexSet with target_table.RowSelection.AsIndexSet().

内容的提问来源于stack exchange,提问作者san

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 07:52:53