Created
July 12, 2017 09:08
-
-
Save phaistonian/315ac33dfd100436df77413314ba7d48 to your computer and use it in GitHub Desktop.
A little helper to make saving to Google Sheets super easy
This file contains 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
// Instructions here: https://gist.github.com/phaistonian/1f3de5f8bc0acaf4c48334d0a9384d28 | |
const saveToGSheet = (urlOrId, data) => { | |
const url = urlOrId.indexOf('https') === -1 | |
? `https://script.google.com/macros/s/${urlOrId}/exec` | |
: urlOrId; | |
const formData = new FormData(); | |
Object.keys(data) | |
.forEach(key => { | |
formData.append(key, data[key]); | |
}); | |
return new Promise(async (resolve, reject) => { | |
try { | |
const result = await fetch(url, { | |
method: 'POST', | |
body: formData, | |
}); | |
const json = await result.json(); | |
if (json.result && json.result === 'success') { | |
return resolve(true); | |
} else { | |
return reject(new Error('Result is not success.')); | |
} | |
} catch (error) { | |
return reject(error); | |
} | |
}); | |
}; | |
export default saveToGSheet; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment