Skip to content

Instantly share code, notes, and snippets.

@namieluss
Created March 8, 2020 04:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save namieluss/7f73785fb21561da0c90019f14f65519 to your computer and use it in GitHub Desktop.
Save namieluss/7f73785fb21561da0c90019f14f65519 to your computer and use it in GitHub Desktop.
Firebase Cloud Functions with Google Spreadsheet
const { authorize } = require("./authorize");
const { google } = require("googleapis");
exports.handler = (req, res) => {
res.set("Access-Control-Allow-Origin", "*");
res.set("Access-Control-Allow-Methods", "POST");
const spreadsheetId = String(req.body.ssid);
const range = String(req.body.sheetRange);
if (spreadsheetId && range) {
const updateWorkSheet = auth => {
const sheet = google.sheets({ version: "v4", auth });
const valueInputOption = "USER_ENTERED";
const resource = { values: req.body.data };
const request = {
range,
spreadsheetId,
valueInputOption,
resource
};
// sheet.spreadsheets.values.update
// sheet.spreadsheets.values.append
sheet.spreadsheets.values.update(request, (err, response) => {
if (err) {
res.status(501).json({
msg: "update worksheet failed",
err: err
});
} else {
res.status(200).json({
msg: "remove worksheet successful",
res: response
});
}
});
};
authorize(updateWorkSheet);
} else {
res.status(404).json({
msg: "spreadsheet and range required"
});
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment