Googlesheet APIv4 getting empty cells

后端 未结 9 1496
我寻月下人不归
我寻月下人不归 2021-01-03 22:03

I have a googlesheet where a column may contain no information in it. While iterating through the rows and looking at that column, if the column is blank, it\'s not returni

相关标签:
9条回答
  • 2021-01-03 22:25

    Another option is iterating through the returned rows, checking the length of the row and appending whatever data you were expecting to be returned. I found this preferable to adding junk data to my dataset.

    0 讨论(0)
  • 2021-01-03 22:26

    If last cell in row has a value then the row will be returned fully for example:

    Rows:

    |Nick|29 years|Minsk|
    |Mike|        |Pinsk|
    |Boby|        |     |
    

    Return:

    [
      ["Nick", "29 years", "Minsk"],
      ["Mike", "", "Pinsk"]
      ["Boby"]
    ]
    

    So when you add a new line with empty cells instead of empty("" or null) just use space " "

    And then when you read values just map all items from space " " to empty ""

    Rows:

    |Nick|29 years|Minsk|
    |Mike|        |Pinsk|
    |Boby|        |"  " |
    

    Return:

    [
      ["Nick", "29 years", "Minsk"],
      ["Mike", "", "Pinsk"]
      ["Boby", "", " "]
    ]
    
    0 讨论(0)
  • 2021-01-03 22:34

    The only solution I could find is writing your own function:

    def _safe_get(data, r, c):   
        try:
            return data[r][c]
        except IndexError:
            return ''
    
    def read(range_name, service):
        result = service[0].spreadsheets().values().get(spreadsheetId=service[1],
                                                    range=range_name).execute()
        return result.get('values', [])
    
    def safe_read(sheet, row, col, to_row='', to_col='', service=None):
            range_name = '%s!%s%i:%s%s' % (sheet, col, row, to_col, to_row)
            data = read(range_name, service)
    
        if to_col == '':
            cols = max(len(line) for line in data)
        else:
            cols = ord(to_col.lower()) - ord(col.lower()) + 1
        if to_row == '':
            rows = len(data)
        else:
            rows = to_row - row + 1
    
        return [[_safe_get(data, r, c)
                 for c in range(cols)]
                for r in range(rows)]
    
    0 讨论(0)
提交回复
热议问题