Last active
March 24, 2021 17:35
-
-
Save rproenca/a3f3a6893b86f83e0163930611f37842 to your computer and use it in GitHub Desktop.
An ad hoc solution for mapping smartsheet columns ids to columns titles and vice-versa given an arbitraty object as input
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Usage | |
----- | |
$ npm init | |
$ npm install axios | |
Replace variables "token", "smartsheetId" and "payload" | |
$ node smartsheet_column_mapper.js | |
Sample payloads | |
--------------- | |
Replaces columns IDs by columns titles: | |
const payload = { | |
"foo": 2029574552071291, // => "foo" : "column title A" | |
"bar": 8211186521574900, // => "bar" : "column title B" | |
"baz": { | |
"lorem": 1217136521894920, // "lorem": "column title C" | |
} | |
} | |
Replaces columns titles by column IDs: | |
const payload = { | |
"foo": "column title A", // => "foo" : 2029574552071291 | |
"bar": "column title B", // => "bar" : 8211186521574900 | |
"baz": { | |
"lorem": "column title C", // "lorem": "1217136521894920" | |
} | |
} | |
*/ | |
const axios = require('axios'); | |
const token = '<SMARTSHEET_TOKEN>'; | |
const smartsheetId = '<SMARTSHEET_ID>'; | |
const payload = {}; | |
axios.defaults.headers.common = { 'Authorization': `Bearer ${token}` }; | |
const map = (obj, sheet) => { | |
for (const prop in obj) { | |
const val = obj[prop]; | |
if (typeof val === 'object') return map(val, sheet); | |
if (typeof val === 'number') { | |
const col = sheet.columns.find(c => c.id === val); | |
obj[prop] = col ? col.title : '<NOT FOUND>'; | |
} else { | |
const col = sheet.columns.find(c => c.title === val).id; | |
obj[prop] = col ? col.id : '<NOT FOUND>'; | |
} | |
} | |
}; | |
(async () => { | |
const { data: sheet } = await axios.get(`https://api.smartsheet.com/2.0/sheets/${smartsheetId}`); | |
map(payload, sheet); | |
console.log(JSON.stringify(payload, null, 4)); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment