尝试在给定条件下删除多行时超出最大执行错误

时间:2017-10-09 10:56:45

标签: google-apps-script google-spreadsheet-api

目前,我正尝试使用以下代码编写代码:

bin_ce = K.binary_crossentropy(G, P)
loss = K.sum(tf.where(tf.equal(M, 1), bin_ce, 0))

不幸的是,这似乎并不是实现我需要的最佳方式,因为我正在处理的工作表介于5-10K行之间,因此脚本超出了最大执行时间。

这可以以更好,更快,更智能的方式完成吗?怎么样?

2 个答案:

答案 0 :(得分:2)

试试这个:

Public Sub updateDuplicatesWithInfoFromIDs()
    Dim IDs As Variant 'array
    Dim DuplicatesRange As Range  'we'll use this for writing after
    Dim Duplicates As Variant 'array

    Dim IndexID As Long 'inner loop index
    Dim IndexDuplicate As Long 'outer loop index
    Dim IDsUbound As Long

    IDs = Range(Sheets("Sheet2").Range("E10"), Sheets("Sheet2").Range("N10").End(xlDown)).Value2 '10 columns, array
    Set DuplicatesRange = Range(Sheets("Sheet1").Range("E15"), Sheets("Sheet1").Range("N3000")) '10 columns, range
    Duplicates = DuplicatesRange.Value2 ' 10 columns, array

    IDsUbound = UBound(IDs) 'we need to set this only once

    For IndexDuplicate = 1 To UBound(Duplicates) 'ubound is set only once
        For IndexID = 1 To IDsUbound
            If Duplicates(IndexDuplicate, 1) = IDs(IndexID, 1) Then
                Duplicates(IndexDuplicate, 2) = IDs(IndexID, 2)
                Duplicates(IndexDuplicate, 10) = IDs(IndexID, 10)
                Exit For 'skip to next duplicate
            End If
        Next
    Next

    'We use the range to write updated values back
    DuplicatesRange.Value2 = Duplicates
End Sub

最好从底部删除到顶部。

答案 1 :(得分:1)

这是使用JS中的every()方法的解决方案。它可以帮助您避免手动循环和测试每个条件。

function deleteRow() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();

  // Get all the data as a 2D array
  var data = sheet.getDataRange().getValues();

  // Go through each row in the array
  for(var i=0; i<data.length; i++) {
    var row = data[i];

    // Test the row for *every* cell having 0.
    var toDelete = row.every(isZero);

    // If all columns in the row === 0, delete the row.
    if(toDelete) { sheet.deleteRow(i+1) }
  }
}

// Test 
function isZero(element, index, array) { 
  // returns true if each element in the array (the columns) === 0
  return element === 0 
}