0

How do I add a custom function to Google Sheets from the Google Workspace add-on environment?

Note that this question is not about adding a custom functions for an Editor Add-on. This question is about building a Google Workspace add-on.

For reference, assume the custom function I want to add is as follows:

Custom function
function GETTAX( price, rate, ) {
  const tax = price * rate;
  const dollarUS = Intl.NumberFormat("en-US", {
    style: "currency",
    currency: "USD",
  });
  const result = dollarUS.format( tax, );
  return result;
}

How can I add a custom function to Sheets when my Google Workspace add-on is open?

Makyen
  • 31,849
  • 12
  • 86
  • 121
Let Me Tink About It
  • 15,156
  • 21
  • 98
  • 207
  • 2
    I just posted a question on meta about including ChatGPT conversations in questions -> https://meta.stackoverflow.com/q/422917/1595451. I not included the link to this question to prevent having a negative "[Meta effect](https://meta.stackoverflow.com/q/269349/1595451)" – Rubén Jan 28 '23 at 19:11
  • 3
    That whole block of ChatGPT conversation is not useful for this question. It's a language model, it doesn't reason, and it's often wrong. It adds a whole lot of useless information to the question and should be rolled back. – Cerbrus Jan 28 '23 at 20:06

1 Answers1

0

Tl;Dr: It's not possible.

Custom functions works only in Editor add-ons, not in Workspace add-ons.


From the Official Docs

Google Developers > Google Apps Script > Guides > Sheets > Custom Functions

Specific page: https://developers.google.com/apps-script/guides/sheets/functions (links not included)

Creating a custom function

To write a custom function:

  1. Create or open a spreadsheet in Google Sheets.
  2. Select the menu item Extensions > Apps Script.
  3. Delete any code in the script editor. For the DOUBLE function above, simply copy and paste the code into the script editor.
  4. At the top, click Save save. Now you can use the custom function.

Sharing

Custom functions start out bound to the spreadsheet they were created in. This means that a custom function written in one spreadsheet can't be used in other spreadsheets unless you use one of the following methods:

  • Click Extensions > Apps Script to open the script editor, then copy the script text from the original spreadsheet and paste it into the script editor of another spreadsheet.
  • Make a copy of the spreadsheet that contains the custom function by clicking File > Make a copy. When a spreadsheet is copied, any scripts attached to it are copied as well. Anyone who has access to the spreadsheet can copy the script. (Collaborators who have only view access cannot open the script editor in the original spreadsheet. However, when they make a copy, they become the owner of the copy and can see the script.)
  • Publish the script as a Google Sheets add-on.

Clarifications:

  1. Google Sheets add-on refers to Editor add-on, not a Workspace add-on. To learn about Add-ons types see https://developers.google.com/apps-script/add-ons/concepts/types.
Makyen
  • 31,849
  • 12
  • 86
  • 121
Rubén
  • 34,714
  • 9
  • 70
  • 166
  • I read the link you provided but I could not find absolute confirmation that custom functions are not possible from Workspace add-ons. Additionally, I ran the question by ChatGPT (see above Edit 1) which seemed to disagree with your answer and states that it *is* possible. I tend to trust a human expert over AI at this point but in this case I don't have clear independent verification despite the documentation link you provided. Highlighting a pull quote from that page for me with definitive language would help clarify. Any thoughts? – Let Me Tink About It Jan 28 '23 at 07:11
  • 1
    @LetMeTinkAboutIt Answer updated. Thoughts? You might try the [Google Apps Script chat room](https://chat.stackoverflow.com/rooms/217630/google-apps-script-chat-community). Nowadays there are very few SME visiting the chat but if the topic is interesting I think that it get replies at least from one of the regulars. – Rubén Jan 28 '23 at 17:28
  • Thank you for the clarifications. I also posted a comment to the GAS chat room here requesting guidance for a workaround strategy... https://chat.stackoverflow.com/transcript/message/55898705#55898705 ... Also, IDK if this is the right place to ask this, but in your opinion, why are the SME not frequenting the chat room anymore? Are they going somewhere else? Is GAS losing popularity? What's the reason?? – Let Me Tink About It Jan 28 '23 at 19:23
  • 1
    @LetMeTinkAboutIt The answer is complex. P.S. I dont participate a lot on chat rooms as I found most of conversations on them hard to follow and derailed too frequently. – Rubén Jan 28 '23 at 19:28
  • Due to the complexity of the answer, [I posted a new question here](https://stackoverflow.com/q/75270440/1640892). – Let Me Tink About It Jan 28 '23 at 19:54