Python gspread:相当于getLastRow()

时间:2015-09-22 23:34:37

标签: python gspread

我目前使用此代码,我认为随着工作表的增长需要的时间越长:

def findLastRow():
    global spreadsheet
    global datastartrow

    print 'rows: ', worksheet.row_count

    lastrow = datastartrow

    while(True):
        val = worksheet.cell(lastrow, datastartrow).value

        if val == '':
            lastrow -= 1

            break

        lastrow += 1

    return lastrow

是否有更高效的方式,如GAS的getLastRow?

1 个答案:

答案 0 :(得分:1)

我认为我找到了一种仍然效率不高的迂回方式,但速度有点快,这是我的新计数测试与从控制台捕获的旧测试:

trying to login to google sheets
logged in
sync time
starting count 1
colvals:  1001
count 1 took:  3.41200017929 seconds

starting count 2
lastrow:  1001
count 2 took:  531.482000113 seconds

这是新旧vs:

def findLastRow():
    global spreadsheet
    global datastartrow
    global datastartcolumn

    t0 = time.time()

    print 'starting count 1'
    colvals = worksheet.col_values(1)
    t1 = time.time()

    print 'colvals: ', len(colvals)

    print 'count 1 took: ', str(t1 - t0) + ' seconds'

    # return len(colvals)

    t0 = time.time()

    print 'starting count 2'

    lastrow = datastartrow

    while(True):
        val = worksheet.cell(lastrow, datastartrow).value

        if val == '':
            lastrow -= 1

            break

        lastrow += 1

    print 'lastrow: ', lastrow

    t1 = time.time()

    print 'count 2 took: ', str(t1 - t0) + ' seconds'

    return lastrow