Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/scripts/google-sheets/.clasp.json
6 changes: 6 additions & 0 deletions scripts/google-sheets/appsscript.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"timeZone": "Asia/Jerusalem",
"dependencies": {},
"exceptionLogging": "STACKDRIVER",
"runtimeVersion": "V8"
}
13 changes: 13 additions & 0 deletions scripts/google-sheets/getSessionToken.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function getSessionToken(apiKey) {
const options = {
method: 'post',
contentType: 'application/json',
headers: {
'x-api-key': apiKey,
},
}
const response = UrlFetchApp.fetch('https://api.mapme.com/auth/key', options)

const parsedResponse = JSON.parse(response)
return parsedResponse.sessionToken
}
14 changes: 14 additions & 0 deletions scripts/google-sheets/publish.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
function publish() {
const config = retrieveConfig()
const sessionToken = getSessionToken(config.apiKey)

const options = {
method: 'post',
contentType: 'application/json',
headers: {
authorization: `Bearer ${sessionToken}`,
},
}

UrlFetchApp.fetch(`https://api.mapme.com/api/scenes/${config.sceneId}`, options)
}
15 changes: 15 additions & 0 deletions scripts/google-sheets/retrieveConfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
function retrieveConfig() {
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('config')

const SCENE_ID_ROW = 1
const API_KEY_ROW = 2
const VALUE_COLUMN = 2

const sceneId = sheet.getRange(SCENE_ID_ROW, VALUE_COLUMN)
const apiKey = sheet.getRange(API_KEY_ROW, VALUE_COLUMN)

return {
sceneId: sceneId.getValue(),
apiKey: apiKey.getValue(),
}
}
101 changes: 101 additions & 0 deletions scripts/google-sheets/sync.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
class Map {
constructor(sheetName) {
this.sheetName = sheetName
const config = retrieveConfig()
this.sceneId = config.sceneId
this.sessionToken = getSessionToken(config.apiKey)
}

addSection(row) {
const [
,
status,
shouldDelete,
id,
name,
description,
categories,
address,
latitude,
longitude,
pitch,
zoom,
bearing,
actionText,
actionUrl,
media,
] = row

const data = {
sectionData: {
id: Utilities.getUuid(),
name,
description,
address,
mapView: {
center: {
lat: latitude,
lng: longitude,
},
zoom,
bearing,
pitch,
// 'centerZoom' | 'bounds' | 'autofit'
mode: 'centerZoom',
},
callToAction: {
url: actionUrl,
title: actionText,
// '_modal' | '_self'
// target: "_modal"
},
},
sceneId: this.sceneId,
categoryIds: [],
}

const options = {
method: 'post',
contentType: 'application/json',
headers: {
authorization: `Bearer ${this.sessionToken}`,
},
payload: JSON.stringify(data),
}
UrlFetchApp.fetch('https://api.mapme.com/api/sections', options)
}

sync() {
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(this.sheetName)
const table = sheet.getDataRange().getValues()

const VALUES_START_ROW = 5
const STATUS_COLUMN = 2
const ERROR_COLUMN = 17

const values = table.slice(VALUES_START_ROW)

for (const i in values) {
const value = values[i]
const [, status] = value
if (status === 'new') {
const row = VALUES_START_ROW + 1 + parseInt(i)
const statusCell = sheet.getRange(row, STATUS_COLUMN)

try {
this.addSection(value)
statusCell.setValue('sync')
} catch (e) {
statusCell.setValue('error')
const errorCell = sheet.getRange(row, ERROR_COLUMN)
errorCell.setValue(e.message)
}
}
}
}
}

function sync() {
const map = new Map('ADD or SYNC')
map.sync()
}