I'm using the 3rd party importJSON()
function, and I needed a way to make it calculate only when I explicitly requested it. Thanks to this question, I now have a way to do that, but a big problem remains.
Now, any time a calculation is performed (which is On Change), importJSON either calculates if I have it set to do so, or wipes out the existing data that was calculated last time. This means my data is constantly disappearing on me in the middle of trying to use my spreadsheet.
Is there any way to make importJSON()
return if not set to calculate, but not erase data in previously-calculated cells?
Best Answer
Generally, multiple import-statements of whatever kind are a path to frustration. I offer a general solution, not specific to ImportJSON: it is a way to always invoke a specific custom function explicitly, using the menu. The code is below, and here is the explanation.
ImportJSON
, usemanualImportJSON
functions, with the same parameters.ImportJSON
in the script as it originally was, but it will no longer be called as a custom function. For the sake of completeness I included a demo version of this function here; it only spits out the parameters and a few numbers to show that it works.onOpen
function. It calls the functionactuallyImportJSON
, which finds allmanualImportJSON
functions in the sheet, retrieves parameters from it, and passes them to the realImportJSON
function. Then it puts the imported data in the sheet, below the cell withmanualImportJSON
, so as not to overwrite it. (Note: the data will mercilessly overwrite anything, unlike the output of custom functions.)Specifically,
/^=manualImportJSON/i.test(formulas[i][j])
is the part that tests for "manual" function andeval("ImportJSON" + formulas[i][j].slice(17))
is the part that evaluates the real ImportJSON function. The number 17 is just the length of=manualImportJSON
, the characters that are being replaced byImportJSON
.Limitations of this approach: (a) the arguments of custom function must be literals, not formulas or cell references; (b) the custom function cannot be a part of formula. See below for an alternative.
Cell references/formulas as arguments
If the original custom function had such parameters, the approach above will not work since a JavaScript function has no idea what "B2" or such mean. Here is a modified approach.
Instead of
=manualimportJSON(url, query, parseOptions)
, type only the stringmanualImportJSON
in a cell, and put its parameters in separate cells to the right of it. (So, if B2 has "manualImportJSON", then C2 has the URL, D2 has the query, and E2 has parse options.)Use the modified version of
actuallyImportJSON
below.This version gets all values from the spreadsheet, rather than formulas. If a value matches "manualImportJSON" (case insensitive) than the contents of three cells to the right are passed as parameters to
ImportJSON
; the rest goes as above.