4

Trying out the new library feature in a Google spreadsheet. I have include a library with the identifier of "Test" and the library implements the function "foo()"

entering =Test.foo() into a spreadsheet cell gives the error : "unknown function name TEST.FOO"

If I create a function in my spreadsheet to wrap the library function:

function foo()
{
  return Test.foo();
}

then use =foo() in my speadsheet cell, all is well. Creating wrapper functions for all library functions so they can be used in a spreadsheet cell makes using libraries less than ideal. Is there a way to call a library function from a spreadsheet cell?

4

2 に答える 2

4

この 1 つの汎用ラッパー関数を貼り付けると、任意のライブラリ関数を呼び出すことができる回避策があります。次に、スプ​​レッドシートからこれを呼び出すことができます。

たとえば、関数 add(x, y) を持つ MyLib というライブラリがある場合 (x がセル A1 にあり、y がセル A2 にあるとします)、次のように呼び出すことができます: =LIB_FUNC("MyLib", "add" 、A1、A2)。

少し醜いですが、少なくともこの 1 つの関数を貼り付けるだけで、任意のライブラリ関数にアクセスできます。これは、ラッパー関数を呼び出すときにスコープ内にある「this」オブジェクトの文書化されていない構造に依存することに注意してください。これが時間の経過とともに壊れる可能性はわずかです。これをアドオンとして公開できるかどうかを確認するかもしれません。

function LIB_FUNC(libraryName, functionName) {
  var result;
  var lib = this[libraryName];
  var extraArgs = [];

  if (lib) {
    var func = lib[functionName];

    if (func) {
      if (arguments.length > 2) {
        extraArgs = Array.apply(null, arguments).slice(2);
      }

      result = func.apply(this, extraArgs);
    } else {
      throw "No such function: " + functionName;
    }
  } else {
    throw "No such library: " + libraryName;
  }

  return result;
}
于 2016-12-22T15:59:57.793 に答える
4

現在、これらのライブラリ関数をセルからカスタム関数として直接呼び出す方法はありません。あなたが行ったように、ラッパーを作成することは、現在これを行う方法です。ライブラリ関数をカスタム関数として直接呼び出せるようにしたい場合は、Issue Trackerで問題として提起してください。

于 2012-05-25T19:19:31.210 に答える