简体   繁体   English

Google表格脚本-太慢

[英]Google Sheets Script - Too slow

I have google sheets set up as a database for my raspberry pi to send readings of temp, humidity etc. It is doing that just fine however for some reason google sheets will create duplicate huge arrays of the data in the same sheet. 我已经将Google表格设置为我的树莓派的数据库,以发送温度,湿度等读数。这样做很好,但是出于某些原因,Google表格会在同一张表格中创建大量重复的数据。

To solve this problem I have been using the standard removeDuplicates google script code that I found (see below). 为了解决这个问题,我一直在使用我发现的标准removeDuplicates google脚本代码(请参见下文)。 The problem with this is that sometimes google sheets will copy over 100,000 rows of data to duplicate that a couple of time. 问题是有时Google表格会复制100,000行以上的数据来复制两次。 This means there is a LOT of duplicates for the script to sort out. 这意味着该脚本有很多重复项可以整理。

I have read that maybe the issue is with so many duplicates, having to write individually to sort each duplicate could be taking much too long and I should try to get a list ALL of the duplicates in one go and then do ALL of the removing of the duplicates in one go. 我已经读过,也许是重复的太多了,不得不单独写出来对每个重复进行排序可能会花费太长时间,我应该尝试一次性获得所有重复列表,然后再删除所有一式两份的重复项。 Now I am rather novice at this so I am not sure how I would achieve this. 现在,我在这方面是新手,所以我不确定如何实现。

function removeDuplicates() {
    // [START apps_script_sheets_sheet]
    var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
    var sheet = spreadsheet.getSheetByName('RawData');
    var data = sheet.getDataRange().getValues();
    // [END apps_script_sheets_sheet]
    // [START apps_script_sheets_new_data]
    var newData = [];
    // [END apps_script_sheets_new_data]
    for (var i in data) {
      var row = data[i];
      var duplicate = false;
      for (var j in newData) {
        if (row.join() == newData[j].join()) {
          duplicate = true;
        }
      }
      // [START apps_script_sheets_duplicate]
      if (!duplicate) {
        newData.push(row);
      }
      // [END apps_script_sheets_duplicate]
    }
    // [START apps_script_sheets_clear]
    sheet.clearContents();
    sheet.getRange(1, 1, newData.length, newData[0].length).setValues(newData);
    // [END apps_script_sheets_clear]
  }
  // [END apps_script_sheets_remove_duplicates]

There only errors is the code does not finish in the 6 minutes of allocated time. 唯一的错误是代码未在分配的6分钟内完成。

How do I solve the problem? 我该如何解决这个问题?

try to specify number of rows, may be its trying to include remaining empty rows. 尝试指定行数,可能是它试图包含剩余的空行。

function removeDuplicates() {
    // [START apps_script_sheets_sheet]
    var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
    var sheet = spreadsheet.getSheetByName('RawData');
    var startRow = 1;  // First row of data to process
    var numRows = 4000;
    var data = sheet.getRange(startRow, 1, numRows, 1000)

    // [END apps_script_sheets_sheet]
    // [START apps_script_sheets_new_data]
    var newData = [];
    // [END apps_script_sheets_new_data]
    for (var i in data) {
      var row = data[i];
      var duplicate = false;
      for (var j in newData) {
        if (row.join() == newData[j].join()) {
          duplicate = true;
        }
      }
      // [START apps_script_sheets_duplicate]
      if (!duplicate) {
        newData.push(row);
      }
      // [END apps_script_sheets_duplicate]
    }
    // [START apps_script_sheets_clear]
    sheet.clearContents();
    sheet.getRange(1, 1, newData.length, newData[0].length).setValues(newData);
    // [END apps_script_sheets_clear]
  }
  // [END apps_script_sheets_remove_duplicates]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM