GithubHelp home page GithubHelp logo

appscurrystep2's Introduction

Using npm modules in Google Apps Script

An example project where we bundle npm modules and use ES6 code in Google Apps Script

See the full article here

Usage

Install clasp command line tool

npm install @google/clasp -g

Then login to your Google account

clasp login

Open terminal and clone this project.

You have to create an Apps Script project to run this code.

clasp create --type standalone --title "Apps Script with Webpack and babel"

Now build the project and upload to your newly created Apps Script project

npm install
npm run deploy

Why use npm in Apps Script?

Google Apps Script is quite powerful since it can access other Google services like Google Sheets, Gmail and Google docs. You can take automation to the next level using Google Aps Script.

Being able to use npm modules will make creating and sharing re-usable modules quite easy. This will make it easy to create wrappers for popular services and use those services in your Apps Script project.

In this example project, we are using momentjs and lodash in the Apps Script project.

How is it done?

See the server/lib.js file. It imports the npm module and exports functions that use the imported packages. The api.js has a global function doGet() which gets called by Apps Script to show the web page. If you see the webpack.config.js file, notice that the bundle exposes a global variable AppLib where you can get the functions exported in the bundle.

Follow the complete article here

Disclaimers

The author does not represent nor associated with Google in any way. This is project is only for learning purposes.

appscurrystep2's People

Contributors

prasanthmj avatar isaacwhyuenac avatar

Stargazers

 avatar Dmytro2V avatar Aldy Adam avatar M.A.ASHARAF ALI avatar François Séguin avatar Fabrizzio Soares avatar Robotghost avatar Patrick Chong avatar Chris Tales avatar  avatar Bikash Lalani avatar Antonio Roa-Valverde avatar ivan avatar Leonardo Santos avatar Chris McDermut avatar Andy Edwards avatar Jon Repp avatar  avatar Prashant Yashpal avatar Nirmal avatar  avatar Job Diógenes Ribeiro Borges avatar  avatar Laura Taylor avatar

Watchers

James Cloos avatar  avatar  avatar

appscurrystep2's Issues

SyntaxError: Invalid property ID.

Hi there, your tutorial works as expected, but I tried to import another npm module (tensorflow.js) on the exact same way. But as soon as I import this module with import * as tf from '@tensorflow/tfjs'; it throws the error SyntaxError: Invalid property ID. (Line 110, File "lib.bundle") Maybe you have any idea how to make the package compatible

Webpack uses GAS keywords when compiling code

Hey there,

thanks a lot for making this tutorial. Very helpful.

When I tried customizing your project I have noticed that some npm modules won't get compiled in a GAS friendly way.

Here's my setup:

  1. Cloned your repo & npm installed all dependencies
  2. In lib.js I have added:
[...]
import bizSdk from 'facebook-nodejs-business-sdk';


function getActId(){
	const accessToken = '<VALID_ACCESS_TOKEN';
	
	const FacebookAdsApi = bizSdk.FacebookAdsApi.init(accessToken);
	const AdAccount = bizSdk.AdAccount;
	
	const account = new AdAccount(`act_${<ACCOUNT_ID>}`);
	return account.id

}
[...]
export {
    getObjectValues,
    getTodaysDateLongForm,
    getDaysToAnotherDate,
	printSomeNumbers,
	getActId
}; 

I've tried calling the function in the api.js Logger.log(AppLib.getActId()) but am getting the following error:
SyntaxError: Missing name after . operator. (line 2385, file "lib.bundle")

I believe this might somehow be related to the other SyntaxError issue.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.