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 104 of file transport10.cpp.

106{
107 QAxObject* sheet = sheets->querySubObject( "Item( string )", sheetName.c_str() );
108 GAMSParameter param = db.addParameter(paramName, paramText, set);
109
110 QAxObject* usedrange = sheet->querySubObject( "UsedRange");
111 QAxObject * columns = usedrange->querySubObject("Columns");
112 int intCols = columns->property("Count").toInt();
113
114 for (int i = 1; i <= intCols; i++) {
115 std::string name = sheet->querySubObject("Cells( int, int )", 1, i)->dynamicCall("Value()").toString().toStdString();
116 double value = sheet->querySubObject("Cells( int, int )", 2, i)->dynamicCall("Value()").toDouble();
117 set.addRecord(name);
118 GAMSParameterRecord rec = param.addRecord(name);
119 rec.setValue(value);
120 }
121 return param;
122}
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 135 of file transport10.cpp.

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