Skip to content

Instantly share code, notes, and snippets.

@phaistonian
Created July 12, 2017 09:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phaistonian/315ac33dfd100436df77413314ba7d48 to your computer and use it in GitHub Desktop.
Save phaistonian/315ac33dfd100436df77413314ba7d48 to your computer and use it in GitHub Desktop.
A little helper to make saving to Google Sheets super easy
// 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