How do I deploy a simple Google App Script Library to a Google Sheet

Daniel Vaca

enter image description here

I created a Google App Script named 'MySimpleSharedCode' with the following code:

var MySimpleSharedCode = MySimpleSharedCode || {};

MySimpleSharedCode.sayHello = function() {
  return "hello from MySimpleSharedCode";
}

function sayHello2() {
  var result = MySimpleSharedCode.sayHello();
  console.log(result);
  return result;
}

I deployed this script as a Library.

Then created a new Google App Script named 'BorrowerCode'. Added the 'MySimpleSharedCode' library and then following code:

function sayHello3() {
  var result = MySimpleSharedCode.sayHello();
  console.log(result);
  return result;
}

Now when I run sayHello2() in 'MySimpleSharedCode' I see in the logs what I would expect:

hello from MySimpleSharedCode

But when I run sayHello3() in 'BorrowerCode' I get an error

TypeError: MySimpleSharedCode.sayHello is not a function
sayHello3   @ Code.gs:2

What am I doing wrong?

In the screenshot above, looks like the library got added. But somehow I'm not able to access the code in the library?

Tanaike

In your situation, when MySimpleSharedCode is installed in BorrowerCode as a library of identification of MySimpleSharedCode, MySimpleSharedCode has the objects of MySimpleSharedCode and sayHello2.

In your following sample script of BorrowerCode,

function sayHello3() {
  var result = MySimpleSharedCode.sayHello();
  console.log(result);
  return result;
}

MySimpleSharedCode has no object of sayHello(). By this, such an error occurs. If you want to run the function of sayHello of MySimpleSharedCode from BorrowerCode, please modify it as follows.

function sayHello3() {
  var result = MySimpleSharedCode.MySimpleSharedCode.sayHello();
  console.log(result); // hello from MySimpleSharedCode
  return result;
}

For example, if you want to call sayHello using MySimpleSharedCode.sayHello() from BorrowerCode, how about the following modification?

MySimpleSharedCode

var MySimpleSharedCode = MySimpleSharedCode || {};

var sayHello = function () {
  return "hello from MySimpleSharedCode";
};

MySimpleSharedCode.sayHello = sayHello;

function sayHello2() {
  var result = MySimpleSharedCode.sayHello();
  console.log(result);
}

BorrowerCode

function sayHello3() {
  var result = MySimpleSharedCode.sayHello();
  console.log(result);
}
  • By this modification, when sayHello2() is run at MySimpleSharedCode, hello from MySimpleSharedCode is shown. And, when sayHello3() is run at BorrowerCode, hello from MySimpleSharedCode is shown.

Reference:

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I get the true range when editing multiple rows on Google Sheet via Google App Script

How do I get arrays mapped in a row in google sheet using JavaScript/Google App script

How do I automatically archive a google sheet with a google script?

How can I write the data in Firestore to Google Sheet with app script?

Google App Script, updates to sheet are not being found by rest of script. How do I make it include updates previously done in the script?

How to refer to other sheet in google app script?

App Script Google sheet - button for pasting data values simple form

How to assign Sheet with Sheet CodeName in Google Sheets App Script

How does Google App Script to set a formula to Google sheet cell

How to deploy add-on of google app script in Google Workspace Marketplace

How can I assign a Google Sheet script to only one sheet?

How do I replace text in a google sheet?

bound standalone google app script to google sheet

How do I send Google App Script email using HTML

How do I run custom python script in Google App engine

How do I send email to multiple recipients in google app script

How Do I Get to Print A and B in Google App Script?

How can i secure app script from the user who is going to use the google sheet?

How Can I add comment on a cell using google sheet app script

How to do simple google script api with php

Gapps Script in Google Sheet Is Not Scrolling No Matter What I Do

How do I download a Google Sheet with Google Picker all in JavaScript?

Convert Simple Excel Macro to Google Sheet Script

How to calculate remaining time in google sheet app script

how is a sheet name referenced in google app script macro?

How to create a true new copy of an array in App Script for Google Sheet?

How to skip Row in the following App Script in Google Sheet?

Google Sheet App Script - How to only setValues only unique values

App Script: how to return TotalRows of a BigQuery-Request in Google Sheet