How to use .find() to locate a cell and start updating cells in rows below in loop?
Asked Answered
O

1

6

I'm automating filling out a Google Sheet with data taken from CSV. For the automation, I want to be able to use .find() to locate a specific cell value that is used as a reference for where to start updating cells. To better explain:

enter image description here

My code uses .find('Cafe Crepe') to locate the rows and columns belonging to the restaurant 'Cafe Crepe'. In the sheet there are multiple restaurants with the same format for orders, sub total, etc. beneath.

def matchAndWriteFinalCSV(self, sheet, restaurant):
        '''
        Match orders from Ecwid csv to restaurant in Delivery csv
        Write to Final csv
        '''
        print("WRITE START")
        cell = sheet.find(f"{restaurant}")
        filtered_list = [] 
        print("WRITE SHEET")
        print(f"ROW {cell.row} COL {cell.col} CELL {cell}")
        sheet.update('R5', "TEST")

UPDATE

To illustrate what the result should be:

enter image description here

I decided to go for creating a list of dictionaries of orders(order num, sub total, tx, etc). Using a for loop, I divide the task of writing/updating the google sheet by restaurant. In the example for this question: my code takes all orders belonging to 'Cafe Crepe' and initates to write/update the order #, sub total, tax, etc. fields.

for restaurant_name, restaurant_orders in orders_per_restaurant.items():
            new_row = 5
            for order in restaurant_orders:
                print(order)
                restaurant = restaurant_name
                cell = sheet.find(f"{restaurant}") 
                print(f"ROW {cell.row} COL {cell.col} CELL {cell}") 
                subtotal = cell.col + 1 
                tax = cell.col + 2
                new_cellrow = cell.row + new_row
                write_cell_start = f"R{str(new_cellrow)}C{str(cell.col)}"
                write_subtotal = f"R{str(new_cellrow)}C{str(subtotal)}"
                sheet.update(write_cell_start, order['order'])
                sheet.update(write_subtotal, order['sub-total'])
                new_row += 1
            new_row =  5
            time.sleep(100)

This works but now I can't get this code to run beyond a certain point without getting "Quota exceeded for quota metric 'Write requests' and limit 'Write requests per minute per user' of service 'sheets.googleapis.com'. I'm trying to understand how I can achieve the same thing with batch_update(). How can I work around exceeding the request per minute rate in my code?

Oakland answered 12/4, 2021 at 22:24 Comment(9)
Unfortunately, from I want to be able to .find() to find the restaurant cell and start updating in the cell highlighted blue in the example above. How can I use cell = sheet.find(f"{restaurant}") as a reference to start updating the cells 5 rows below?, I cannot understand about your goal. I apologize for my poor English skill. Can I ask you about the detail of your goal? If you can do, can you provide the sample input and output situation you expect?Chapfallen
@Chapfallen going to update my question to be more clear on my goal.Oakland
Thank you for replying. I have to apologize for my poor English skill. Unfortunately, from your updated question, I cannot still understand about your question. But I would like to try to understand it. When I could correctly understand it, I would like to think of the solution. I deeply apologize I cannot resolve your issue soon.Chapfallen
@Chapfallen So I want to be able to identify what is the row and col to use sheet.update and write the order num, sub-total, tax etc under the restaurant name cell. So by using .find() I get, for example, ROW 2 COL 28 CELL <Cell R2C28 'Cafe Crepe'>. I can't figure out how to use this to then start updating the cells for order num, sub-total, etc below the restaurant name cell that is located using .find(). Maybe this helps clear up what I'm trying to do.Oakland
Thank you for replying and adding more information. I have a question. Your sample image is the sample input situation? If my understanding is correct, can you provide the sample output situation when the sample input situation is used? By this, I would like to try to understand about your goal.Chapfallen
@Chapfallen The logic is to find cells that need to be filled with restaurant data for order num, sub-total, etc. There are multiple restaurants in the sheet, so I need to use .find() to locate the the fields for that particular restaurant (in this case Cafe Crepe).Oakland
@Chapfallen I added a sample of what the output should be. As you can se the fields for order num, sub-total, etc, are filled.Oakland
Thank you for additional information. You want to retrieve the range of the header of "Cafe Crepe" by searching the value of "Cafe Crepe". Namely, to retrieve the range of header of "Cafe Crepe" is your goal in this question? Is my understanding correct? If my understanding is correct, are there multiple tables like "Cafe Crepe" in a sheet? If it's so, the structures of each table are the same?Chapfallen
Let us continue this discussion in chat.Oakland
M
0

This should steer you where you want to go:

iRow = Sheet.cells.find("searchString").Row
iCol = Sheet.cells.find("searchString").Column

newCell = Sheet.cells(iRow,iCol).offset(iiRow, iiCol)

iiRow and iiCol can be iterators you define in your new loops that use iRow, iCol as their baseline reference point.

Moisten answered 16/4, 2021 at 11:58 Comment(6)
To appy this in my code, in iRow I should search for the restaurant name and in iCol it would be the fields like for example order num?Oakland
Well, I guess you could - but I had intended iRow & iCol should return from .find({Restaraunt}) - then if, say your data you want to work with is 5 columns away from {Restaurant}, you'd use iiCol = 5. (or make a fixed offset of 5 and use an iterator for iiCol for moving across a range of columns.Moisten
Yeah, let me ask you, so a big issue I'm trying to get over is that: for this automation, I'm parsing a CSV sheet full of orders from restaurants. Right now I'm writing to the Google sheet as the orders are parsed. I'm thinking that keeping track of an index per restaurant. But I'm trying to simplify this. Any ideas?Oakland
Without seeing your data I wouldn't know where to start. If you've had a go and its not working out, put up a new question so everyone can have a look.Moisten
Can't figure out how to use offset. If I do sheet.cell(iRow,iCol).offset(1, 5) I get AttributeError: 'Cell' object has no attribute 'offset'Oakland
are you using "cell" or "cells"?Moisten

© 2022 - 2024 — McMap. All rights reserved.