blondie-inc-google-docs-mustaches
TypeScript icon, indicating that this package has built-in type declarations

0.2.8 • Public • Published

logo

google-docs-mustaches

📝Interpolate Google Docs files using mustaches and formatters

How does this work?

google-docs-mustaches will execute requests to the Google Drive and Google Docs APIs to copy the file and interpolate its placeholders using the given data.

Installation

npm install google-docs-mustaches

Basic usage

Create a new Google Doc file and write the following text:

Hello {{ firstname }} {{ lastname | uppercase }}!

You have {{ accounts[0].money }}€ in you account...

Then execute the following code

import Mustaches from 'google-docs-mustaches'
 
const mustaches = new Mustaches({
  token: () => gapi.auth.getToken().access_token
})
 
// ID of the template
const source = '11rGORd6FRxOGERe7fh6LNQfyB48ZvOgQNH6GScK_FfA'
 
// ID of the destination folder
const destination = '18mcqwbaXS8NOqZjztB3OUQAc5_P8M6-l'
 
mustaches.interpolate({
  source,
  destination,
  data: {
    firstname: 'Thibaud',
    lastname: 'Courtoison',
    accounts: [{ money: 1500 }]
  }
})

Documentation

new Mustaches(options: ConstructorOptions)

type AccessToken = string
 
interface ConstructorOptions {
  token: () => AccessToken
}
  • token will be called at every request to the Google apis.

AccessToken must have the following scopes:

See also: How to retrieve the Google token?

mustaches.interpolate(options: ToPdfOptions): ID

This method will interpolate from the source file and put the generated file into the destination folder.

type ID = string
 
export interface InterpolationOptions {
  source: ID
  destination?: ID
  name?: string
  data: Object
  formatters?: Formatters
  export?: MimeType
}
 
interface Formatters {
  [name: string]: Formatter
}
 
type Formatter = (value: any) => string
 
export enum MimeType {
  pdf = 'application/pdf',
  text = 'plain/text'
}
  • source is the ID of the file which will be interpolated.
  • destination is the ID of the destination folder where the new file will be put. If no destination is given, the new file will be put next to the source file.
  • name is the name of the newly created and interpolated google doc file.
  • data is the data given for the interpolation
  • formatters will be used for interpolation
  • export can be specified to export the file after the interpolation

Interpolation

Mustaches

The double brackets notation (also known as mustaches) is used to define placeholders:

My name is {{ firstname }}. Nice to meet you!

During the interpolation, the placeholder will be replaced with the content of the options.data object.

{
  firstname: 'Thibaud'
}
My name is Thibaud. Nice to meet you!

Path notation

You can use nested objects and arrays for the interpolation:

{{ pokemons[1].name }}, I choose you!

With the following options.data

{
  pokemons: [
    {
      name: 'Eevee',
      level: 12
    },
    {
      name: 'Pikachu',
      level: 25
    }
  ]
}

Will become:

Pikachu, I choose you!

Formatters

You can use formatters to print your data and more complex objects any way you want.

There is a number of available formatters, but you can also write your owns

Hi {{ name | uppercase }}. Today is {{ today | printDay }}, tomorrow is {{ tomorrow | printDay }}.

With the following options:

{
  data: {
    name: "Courtoison",
    today: new Date(),
    tomorrow: new Date(new Date().setDate(new Date().getDate()+1))
  },
  formatters: {
    printDay: date => date.toLocaleDateString('en-US',{weekday: 'long'})
  }
}

Will become:

Hi COURTOISON. Today is Tuesday, tomorrow is Wednesday.

Available formatters:

  • lowercase: HeLLo => hello
  • uppercase: wOrLd => WORLD
  • More to come...

How to retrieve the Google token?

If you are using google-docs-mustaches from inside a browser, you can follow this tutorial.

If you are using google-docs-mustaches from in Node.js, you can follow this one.

Note: Your AccessToken must have the following scopes:

Supported environments

We use cross-fetch for compatibility with most environment.

Those are the cross-fetch supported environments:

  • Node 6+
  • React-Native

Browser compat

Readme

Keywords

Package Sidebar

Install

npm i blondie-inc-google-docs-mustaches

Weekly Downloads

4

Version

0.2.8

License

MIT

Unpacked Size

39.9 kB

Total Files

31

Last publish

Collaborators

  • mgladkov