Excel数据导入GAMS:GDX文件无数据但代码无报错,求排查错误
Troubleshooting Empty GDX Files When Importing Excel Data to GAMS
Your code runs without errors but produces empty GDX files. Here are actionable fixes to diagnose and resolve the issue:
1. Fix Excel Range and Header Mismatch
- The
rng=Sheet1!A1:A8760setting expects cell A1 to be a dimension label (e.g., "hour") for parameterPV_Available, with data starting at A2. If A1 contains a numeric value instead of a label, gdxxrw treats all 8760 rows as dimension labels (sincerdim=1requires one label per row), leading to no valid data being imported.- Solution:
- If there’s no header in A1, change the range to
Sheet1!A2:A8760and ensure your setkhas matching labels (e.g.,k /1*8760/). - Or add a header in A1 that aligns with your set
k(e.g., "1", "2", ..., "8760" or a descriptive label like "hour").
- If there’s no header in A1, change the range to
- Solution:
2. Validate Set k Declaration
- Your parameter
PV_Available(k)depends on setkexisting with exactly 8760 elements. Ifkis undefined, has fewer elements, or mismatched labels, the imported data can’t map to the parameter and won’t appear in the GDX file.- Solution: Declare
kexplicitly before your parameters:Set k /1*8760/;
- Solution: Declare
3. Simplify File Paths
- Long paths with spaces and parentheses (e.g., "Fourth Draft (First Run)") can cause gdxxrw to fail silently even if the path is quoted.
- Solution: Move your Excel files to a shorter, space-free directory (e.g.,
C:\GAMS_Data\PV_Available.xlsx) and update the$callcommand with the new path.
- Solution: Move your Excel files to a shorter, space-free directory (e.g.,
4. Enable gdxxrw Debug Logging
- Add
trace=2andlog=filename.logto your$callcommand to capture detailed error information:
Check the generated log file for issues like missing files, invalid ranges, or permission errors.$call gdxxrw "C:\Users\omarr\OneDrive\Desktop\GAMS\Model Development\Fourth Draft (First Run)\Data\PV_Available.xlsx" par=PV_Available rng=Sheet1!A1:A8760 rdim=1 output="PV_Available.gdx" trace=2 log="PV_import.log"
5. Add Error Checking for $call
- Ensure gdxxrw executes successfully by adding an error check:
This will halt the GAMS run if gdxxrw encounters an error, making it easier to identify problems.$call gdxxrw "your_file_path.xlsx" ... $if errorlevel 1 $abort "Failed to process Excel file"
6. Check Excel Sheet for Hidden/Filtered Rows
- Hidden or filtered rows in your Excel range can cause gdxxrw to skip data points. Ensure all rows in
A1:A8760are visible and unfiltered.
内容的提问来源于stack exchange,提问作者Omar
相关产品推荐
相关产品推荐

