Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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