0

我目前使用这个代码,我认为工作表增长得越多,需要的时间越长:

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 这样更有效的方法?

4

1 回答 1

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

这是新旧对比:

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
于 2015-09-23T00:32:41.813 回答