Google Sheets custom function displays "Loading..." forever in mobile app
Asked Answered
C

2

8

I have written a custom function for Google Sheets in Apps Script. The goal is to have a sheet which automatically calculates who owes how much money to whom (e.g. to split a bill).

My sheet looks like this:

enter image description here

The first bill (Restaurant) is to be split among all 5 and the second bill is to be split among all 5 except Peter, because there is no 0 in B3.

The input for my Apps Script function will be cells B1 to F3 (thus, values AND names). The function works fine - it calculates the correct results. I open that spreadsheet via browser (sheets.google.com) AND via my phone app (Google Sheets). However, on my phone it often happens that the result cell (with the formula =calc_debt(B1:F3)) only displays "Loading ...". What's the problem?

For the sake of completeness, here is custom function's code:

function calc_debt(input) {
  var credit = [0, 0, 0, 0, 0]; // credit[0] = Peter, credit[1] = Mark ...
  for (var i = 1; i < input.length; i++) { // starting at i = 1 to skip the first row, which is the names!
    // first: calculate how much everybody has to pay
    var sum = 0;
    var people = 0;
    for (var j = 0; j <= 4; j++) {
      if (input[i][j] !== "") {
        sum += input[i][j];
        people += 1;
      }
    }
    var avg_payment = sum / people;
    // second: calculate who has payed too much or too little
    for (var j = 0; j <= 4; j++) {
      if (input[i][j] !== "") {
        credit[j] += input[i][j] - avg_payment;
      }
    }
  }

  // this function is needed later
  function test_zero (value) {
    return value < 0.00001;
  };

  var res = ""; // this variable will contain the result string, something like "Peter to Mark: 13,8 | Katy to ..."

  while (!credit.every(test_zero)) {
    var lowest = credit.indexOf(Math.min.apply(null, credit)); // find the person with the lowest credit balance (will be minus!)
    var highest = credit.indexOf(Math.max.apply(null, credit)); // find the person with the highest credit balance (will be plus!)
    var exchange = Math.min(Math.abs(credit[lowest]), Math.abs(credit[highest])); // find out by how much we can equalize these two against each other
    credit[lowest] += exchange;
    credit[highest] -= exchange;
    res += input[0][lowest] + " to " + input[0][highest] + ": " + exchange.toFixed(2) + "  |  "; // input[0] = the row with the names.
  }
  return res;
}
Contractor answered 10/10, 2018 at 13:12 Comment(6)
What app? If official one,force stop,clear cache and data of app. open again...let it sync and then open your sheet.Terminate
Apps Script run on Google's servers. If custom functions should work on the mobile app, it certainly should have access to the Internet. Are you using the app with a good Internet connection?Fattish
yes by "google sheets" I meant the official app and clearing the cache + app data does not solve the problem. yes, the internet connection is perfectly fine.Contractor
I have this exact same issue. Sometimes it works perfectly and sometimes is says “Loading...” and doesn’t change.Favorable
I have the exact same issue, were you ever able to resolve it?Fought
hey tyler, no I wasn't. I needed this for a holiday with friends, we ended up using an app called "splitwise". I concluded that the javascript functionality of google sheets must be buggy since I didn't find any errors in my code and in addition I found posts of other people with the same conclusion (google sheets = buggy). Very disappointing ...Contractor
B
4

I'm having a similar issue in the android app that loading a custom formula sometimes just shows 'Loading...', while in the web it always works fine. I've found a workaround to load the formulas in the android app:

Menu - > Export - > Save as - > PDF.

This will take a moment and behind the modal loading indicator you will see that the formulars eventually resolve. You can wait for the export to finish or cancel it as soon as you see your formular was resolved.

Also making the document available offline via the menu toggle could resolve the formulars.

Another thing you could do is using caching in your script. So whenever you use the web version to render more complex formulars the results are being stored and immediately loaded for the mobile app. Unfortunately, the Google cache is limited in time and does invalidate after a few hours. See here for more information: https://developers.google.com/apps-script/reference/cache/

This two things work quite well. However, I'm searching for a better solution. Let me know if you find one.

Basset answered 30/3, 2019 at 10:38 Comment(0)
A
-1

Solved follow the solution provided here .. Menu - > Export - > Save as - > PDF This forces the script to run on mobile and be readable by the mobile sheet

Antihalation answered 5/1, 2020 at 2:46 Comment(1)
This solution is a copy of the other solution is not something new.Earlap

© 2022 - 2025 — McMap. All rights reserved.