Skip to content

Instantly share code, notes, and snippets.

@paxperscientiam
Last active February 17, 2019 05:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paxperscientiam/c2eacbc0ee41557b75f01880945259ce to your computer and use it in GitHub Desktop.
Save paxperscientiam/c2eacbc0ee41557b75f01880945259ce to your computer and use it in GitHub Desktop.
Wrapper function for GAS CardService.newCardBuilder
// Copyright (C) 2018 Christopher David Ramos
export function _Card(data) {
const objCardBuilder = Object.create(objCardMethods)
const card = CardService.newCardBuilder()
const cardHeader = CardService.newCardHeader()
if (data.name) {
card.setName(data.name)
}
Object.keys(data).forEach((el, index) => {
if (el === "title") {
cardHeader.setTitle(data[el])
} else if (el === "subtitle") {
cardHeader.setSubtitle(data[el])
} else if (el === "image") {
cardHeader.setImageUrl(data[el])
}
})
card.setHeader(cardHeader)
objCardBuilder.data = data
objCardBuilder.card = card
return objCardBuilder
}
const objCardMethods = {
addSections(sections: CardSection[]) {
sections.forEach((section: CardSection) => {
if (section !== void 0) {
this.card.addSection(section)
}
})
return this
},
build() {
return this.card.build()
},
setName(name) {
this.card.setName(name)
return this
},
setHeader(cardHeader: CardHeader) {
this.card.setHeader(cardHeader)
return this
},
_() {
return this.card
},
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment