I am using a package to query a list of cells in my Google Sheet and and I'm looking to pass the elements in a different list (forecast) to the values of the Google Sheet list. However, When I run my loops it is passing the last element in my forecast list through each Google Sheet cell in my list. Am I setting up my for loops incorrectly? Is it because of the list element types?
I tried to follow the "Updating Cells" (in batch) section of the library: Gspread
Here is what the two lists look like:
forecasting_range:
Type = <class 'gspread.models.Cell'>
[<Cell R2C3 '179'>, <Cell R3C3 ''>,...,<Cell R32C3 ''>]
forecasted_session_column:
Type = <class 'list'>
['179', '754.6296810633474', '638.2052995605169',...,'607.1381980696942']
Here is my Loop setup:
for cell in forecasting_range:
for forecast in forecasted_session_column:
cell.value = forecast
Output:
[<Cell R2C3 '607.1381980696942'>, <Cell R3C3 '607.1381980696942'>,...,<Cell R32C3 '607.1381980696942'>]
If I understand you correctly, you want cell 1 to have forecast 1, cell 2 to have forecast 2 and so on. If so then use one loop, rather than two.
Each time your outer loop is executing, it sets the value of every cell in the forecasting_range
to each value in the forecasted_session_column
. At the end of each inner loop execution then the cell will only contain the last value.
for i in range(0,len(forecasting_range)):
forecasting_range[i].value = forecasted_session_column[i]
This uses a variable i
to count keep track of an index in both lists so that they will match up the same. Note that this solution expects both lists to be of the same length.