Loading...
Searching...
No Matches
transport10.cpp File Reference

This is the 10th model in a series of tutorial examples. More...

#include "gams.h"
#include <iostream>
#include <vector>
#include <QAxObject>
#include <Windows.h>

Go to the source code of this file.

Functions

GAMSParameter sheetToParameter (QAxObject *sheets, string sheetName, GAMSDatabase db, string paramName, string paramText, GAMSSet set)
 
GAMSParameter sheetToParameter (QAxObject *sheets, string sheetName, GAMSDatabase db, string paramName, string paramText, GAMSSet set1, GAMSSet set2)
 

Detailed Description

This is the 10th model in a series of tutorial examples.

Here we show:

  • How to fill a GAMSDatabase by reading from MS Excel

Definition in file transport10.cpp.

Function Documentation

◆ sheetToParameter() [1/2]

GAMSParameter sheetToParameter ( QAxObject *  sheets,
string  sheetName,
GAMSDatabase  db,
string  paramName,
string  paramText,
GAMSSet  set 
)

Creates an 1 dimensional GAMSParameter and transfers data from an Excel sheet (horizontal ordered)

Parameters
sheetsThe sheets object of an open Excel workbook
sheetNameThe name of the sheet to be read
dbThe GAMSDatabase where the GAMSParameter is created in
paramNameThe name of the new GAMSParameter
paramTextThe explplanatory text of the new GAMSParameter
setThe GAMSSet for GAMSParameter dimension
Returns
The new GAMSParameter in the GAMSDatabase

Definition at line 105 of file transport10.cpp.

107{
108 QAxObject* sheet = sheets->querySubObject( "Item( string )", sheetName.c_str() );
109 GAMSParameter param = db.addParameter(paramName, paramText, set);
110
111 QAxObject* usedrange = sheet->querySubObject( "UsedRange");
112 QAxObject * columns = usedrange->querySubObject("Columns");
113 int intCols = columns->property("Count").toInt();
114
115 for (int i = 1; i <= intCols; i++) {
116 std::string name = sheet->querySubObject("Cells( int, int )", 1, i)->dynamicCall("Value()").toString().toStdString();
117 double value = sheet->querySubObject("Cells( int, int )", 2, i)->dynamicCall("Value()").toDouble();
118 set.addRecord(name);
119 GAMSParameterRecord rec = param.addRecord(name);
120 rec.setValue(value);
121 }
122 return param;
123}
GAMSParameter addParameter(const std::string &name, const int dimension, const std::string &explanatoryText="")
void setValue(const double val)
GAMSParameterRecord addRecord(const std::vector< std::string > &keys)
GAMSSetRecord addRecord(const std::vector< std::string > &keys)

◆ sheetToParameter() [2/2]

GAMSParameter sheetToParameter ( QAxObject *  sheets,
string  sheetName,
GAMSDatabase  db,
string  paramName,
string  paramText,
GAMSSet  set1,
GAMSSet  set2 
)

Creates a 2 dimensional GAMSParameter and transfers data from an Excel sheet

Parameters
sheetsThe sheets object of an open Excel workbook
sheetNameThe name of the sheet to be read
dbThe GAMSDatabase where the GAMSParameter is created in
paramNameThe name of the new GAMSParameter
paramTextThe explplanatory text of the new GAMSParameter
set1The GAMSSet for first GAMSParameter dimension
set2The GAMSSet for second GAMSParameter dimension
Returns
The new GAMSParameter in the GAMSDatabase

Definition at line 136 of file transport10.cpp.

138{
139 QAxObject* sheet = sheets->querySubObject( "Item( string )", sheetName.c_str() );
140 vector<GAMSDomain> sets {set1, set2};
141 GAMSParameter param = db.addParameter(paramName, paramText, sets);
142
143 QAxObject* usedrange = sheet->querySubObject( "UsedRange");
144 QAxObject * columns = usedrange->querySubObject("Columns");
145 int intCols = columns->property("Count").toInt();
146 QAxObject * rows = usedrange->querySubObject("Rows");
147 int intRows = rows->property("Count").toInt();
148
149 for (int j = 2; j <= intCols; j++) {
150 string namej = sheet->querySubObject("Cells( int, int )", 1, j)->dynamicCall("Value()").toString().toStdString();
151 for (int i = 2; i <= intRows; ++i) {
152 string namei = sheet->querySubObject("Cells( int, int )", i, 1)->dynamicCall("Value()").toString().toStdString();
153 GAMSParameterRecord rec = param.addRecord(namei, namej);
154 double value = sheet->querySubObject("Cells( int, int )", i, j)->dynamicCall("Value()").toDouble();
155 rec.setValue(value);
156 }
157 }
158 return param;
159}