Forum Discussion
ChrisCarpenter
Jan 16, 2024Copper Contributor
Python in Excel iteration of sheets
Hello and thank you for any assistance. I have 1 in A1 and 2 in A2 of Sheet2 Additionally, I have the same setup in Sheet3. for i in range(2,4):
value = f"Sheet{i}!A1:A2"
print(x...
- Jan 18, 2024
Python in Excel doesn't work with such kind of indirect references, i.e.
a = "Sheet2!A1:A2" xl(a)returns the same error. xl() accepts direct names of the Excel objects.
ChrisCarpenter
Jan 18, 2024Copper Contributor
Thank you for a response.
I would agree with you if it weren't for the fact that it works after another direct loading of those specific cells line pointing to the sheet.
for i in range(2,4):
value = f"Sheet{i}!A1:A2"
print(xl(value))
Does not work for Sheet2, however,
xl("Sheet2!A1:A2")
for i in range(2,4):
value = f"Sheet{i}!A1:A2"
print(xl(value))
For Sheet2 does work
xl("Sheet2!A1:A3")
for i in range(2,4):
value = f"Sheet{i}!A1:A2"
print(xl(value))
Does not work for Sheet2
Very strange.
peiyezhu
Jan 18, 2024Bronze Contributor
xl("Sheet2!A1:A2")
I guess you are using Excel Python formular rather than run .py script in local python environment.
xl(..) does not belong to python native methods.
So,maybe this result in the strange behaviour.
I guess you are using Excel Python formular rather than run .py script in local python environment.
xl(..) does not belong to python native methods.
So,maybe this result in the strange behaviour.
- ChrisCarpenterJan 18, 2024Copper ContributorYeah I was trying to convert my openpyxl scripts to work within excel so I could have users paste the code and it work without teaching them how to install python or manually adjust code to run it.
As Sergei posted I guess Python in Excel doesn't accept indirect references which is quite unfortunate.- peiyezhuJan 18, 2024Bronze ContributorAs Sergei posted I guess Python in Excel doesn't accept indirect references which is quite unfortunate.
Yes.That is unfortunate.
Alternatively,you can create an online tool with e.g. Django for your users which may privide more flexible methods from python.
Your users can upload Excel files and run full python functions without install python.
I have a similar online tool(https://e.anyoupin.cn/EData/?)to automate data manipulation for fun.