}//btn //TODO: make methods async if possible. private void DetectFieldsAndGetDataHorizontally(int cityRow, int cityCounter, int cityColumn, int maxFieldCount) { var valuesOfTemperature = myDomainFields.GetValues("Temperature"); var valuesOfWindSpeed = myDomainFields.GetValues("Wind Speed"); var valuesOfHumidity = myDomainFields.GetValues("Humidity"); var valuesOfSunset = myDomainFields.GetValues("Sunset"); var valuesOfDescription = myDomainFields.GetValues("Description"); var valuesOfCity = myDomainFields.GetValues("City"); OpenWeatherAPI openWeatherAPI = new OpenWeatherAPI(getAPIKey()); Worksheet currentSheet = Globals.ThisAddIn.getActiveWorkSheet(); for (int fieldIndex_R = cityRow; fieldIndex_R <= cityRow + cityCounter; fieldIndex_R++) { if (IsCity(currentSheet.Cells[fieldIndex_R + 1, cityColumn].Value)) { //isHorizontal = true; Query query = openWeatherAPI.query(currentSheet.Cells[fieldIndex_R + 1, cityColumn].Value); for (int fieldIndex_C = cityColumn + 1; fieldIndex_C <= cityColumn + maxFieldCount; fieldIndex_C++) { if (Array.IndexOf(valuesOfTemperature, currentSheet.Cells[cityRow, fieldIndex_C].Value) >= 0) { currentSheet.Cells[fieldIndex_R + 1, fieldIndex_C].Value = checkedTempUnitIntoQuery(query.Main.Temperature.KelvinCurrent); } if (Array.IndexOf(valuesOfHumidity, currentSheet.Cells[cityRow, fieldIndex_C].Value) >= 0) { currentSheet.Cells[fieldIndex_R + 1, fieldIndex_C].Value = query.Main.Humidity; } if (Array.IndexOf(valuesOfSunset, currentSheet.Cells[cityRow, fieldIndex_C].Value) >= 0) { currentSheet.Cells[fieldIndex_R + 1, fieldIndex_C].Value = query.Sys.Sunset; } if (Array.IndexOf(valuesOfDescription, currentSheet.Cells[cityRow, fieldIndex_C].Value) >= 0)//TODO:Add icon later. { currentSheet.Cells[fieldIndex_R + 1, fieldIndex_C].Value = query.Weathers[0].Description; } if (Array.IndexOf(valuesOfWindSpeed, currentSheet.Cells[cityRow, fieldIndex_C].Value) >= 0)//TODO:Add icon. { currentSheet.Cells[fieldIndex_R + 1, fieldIndex_C].Value = query.Wind.SpeedMetersPerSecond; } } } else if (Array.IndexOf(valuesOfCity, currentSheet.Cells[fieldIndex_R + 1, cityColumn].Value) >= 0) { break; } else { continue; } } currentSheet.Columns.AutoFit(); }
private void Forecast_Button_Click(object sender, RibbonControlEventArgs e) { InitializeMyDomainFields(); Worksheet currentSheet = Globals.ThisAddIn.getActiveWorkSheet(); Microsoft.Office.Interop.Excel.Range xlRange = currentSheet.UsedRange; OpenWeatherAPI openWeatherAPI = new OpenWeatherAPI(getAPIKey()); int maxCol = myDomainFields.AllKeys.Length;//can also be a constant. int lastRow = xlRange.SpecialCells(XlCellType.xlCellTypeLastCell, Type.Missing).Row; int lastColumn = xlRange.SpecialCells(XlCellType.xlCellTypeLastCell, Type.Missing).Column; int fRow = xlRange.Row; int fColumn = xlRange.Column; var valuesOfCity = myDomainFields.GetValues("City"); int cityCounter_h = 0; for (int rowIndex = fRow; rowIndex <= lastRow; rowIndex++) { for (int columnIndex = fColumn; columnIndex <= lastColumn; columnIndex++) { if (Array.IndexOf(valuesOfCity, currentSheet.Cells[rowIndex, columnIndex].Value) >= 0) { int cityRow = rowIndex; int cityColumn = columnIndex; int fCity_row = rowIndex; int fCity_column = columnIndex; while (currentSheet.Cells[fCity_row, cityColumn].Value != null)//and horizontal true { fCity_row++; if (IsCity(currentSheet.Cells[fCity_row, cityColumn].Value) && !IsDataName(currentSheet.Cells[fCity_row, cityColumn].Value)) { cityCounter_h++; } } DetectFieldsAndGetForecastDataHorizontally(cityRow, cityCounter_h, cityColumn, countOfFieldsHorizontal(cityRow, cityColumn, maxCol)); } } } }
private void DetectFieldsAndGetForecastDataHorizontally(int cityRow, int cityCounter, int cityColumn, int maxFieldCount) { var valuesOfTemperature = myDomainFields.GetValues("Temperature"); var valuesOfWindSpeed = myDomainFields.GetValues("Wind Speed"); var valuesOfHumidity = myDomainFields.GetValues("Humidity"); var valuesOfSunset = myDomainFields.GetValues("Sunset"); var valuesOfDescription = myDomainFields.GetValues("Description"); var valuesOfCity = myDomainFields.GetValues("City"); OpenWeatherAPI openWeatherAPI = new OpenWeatherAPI(getAPIKey()); Worksheet currentSheet = Globals.ThisAddIn.getActiveWorkSheet(); for (int fieldIndex_R = cityRow; fieldIndex_R <= cityRow + cityCounter; fieldIndex_R++) { if (IsCity(currentSheet.Cells[fieldIndex_R + 1, cityColumn].Value)) { //isHorizontal = true; FiveDayForecast fivedayforecast = openWeatherAPI.fivedayforecast(currentSheet.Cells[fieldIndex_R + 1, cityColumn].Value); int temperatureColumn = cityColumn + 1; int humidityColumn = cityColumn + 2; int descriptionColumn = cityColumn + 3; int windColumn = cityColumn + 4; int dateColumn = cityColumn + 5; int fieldsRow = cityRow + 1; currentSheet.Cells[cityRow, temperatureColumn].Value = "Temperature"; currentSheet.Cells[cityRow, humidityColumn].Value = "Humidity"; currentSheet.Cells[cityRow, descriptionColumn].Value = "Description"; //TODO:Add icon later. currentSheet.Cells[cityRow, windColumn].Value = "Wind Speed"; //TODO:Add icon. currentSheet.Cells[cityRow, dateColumn].Value = "Date"; foreach (var item in fivedayforecast.forecastList) { currentSheet.Cells[fieldsRow, humidityColumn].Value = item. currentSheet.Cells[fieldsRow, temperatureColumn] = checkedTempUnitIntoQuery(main.Temperature.celsiusCurrent); currentSheet.Cells[fieldsRow, descriptionColumn].Value = weather.Description; currentSheet.Cells[fieldsRow, windColumn].Value = fivedayforecast.Wind.SpeedMetersPerSecond; currentSheet.Cells[fieldsRow, dateColumn].Value = date; fieldsRow++; } } else if (Array.IndexOf(valuesOfCity, currentSheet.Cells[fieldIndex_R + 1, cityColumn].Value) >= 0) { break; } else { continue; } } currentSheet.Columns.AutoFit(); }