New Google Sheets custom functions sometimes display “Loading…” indefinitely

后端 未结 13 1655
没有蜡笔的小新
没有蜡笔的小新 2020-12-24 06:34

SPECIFIC FOR: \"NEW\" google sheets only.

This is a known issue as highlighted by google in the new sheets.

Issues: If you write complex* custom f

13条回答
  •  -上瘾入骨i
    2020-12-24 07:18

    FWIW, I just ran into this and the culprit ended up being a getRange() call that pulled several thousand rows into an array. Periodically it would get hung on the "Loading..." message.

    I worked around it by putting that range into the document cache. It's a little kludgy because the cache only stores strings, not arrays, but you can force it back into an array using .split(',') when you need to access the array.

    (In my case it's a single array. There's probably a way to do it using a double array, either by sending each row or column into its own cache, or reading the cache value back N items at a time, each N becoming its own array.)

    Here's the relevant bit from my code:

    var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("mySheet"); //search the "mySheet" sheet
    // is the big list already in the cache?
    var cache = CacheService.getDocumentCache();
    var cached = cache.get("columnValues");
    if (cached != null) {
       var columnValues = cached.split(','); // take the cached string and make it an array
    } else { // it's not in the cache, so put it there
      var column = 1; // the column with your index
      var columnValues = sheet.getRange(2, column, sheet.getLastRow()).getValues(); // first row is header
      cache.put("columnValues",columnValues,60000); // this forces the array into a string as if you used .join() on it
    }
    

    This is definitely a bug in Apps Script -- getRange() shouldn't hang without a timeout or error message. But at least there's a workaround. Here's the bug I opened against it, where I've also put the full code.gs from my sheet.

提交回复
热议问题