Skip to content

Instantly share code, notes, and snippets.

View alexismp's full-sized avatar

Alexis MP alexismp

View GitHub Profile
@alexismp
alexismp / index.js
Last active November 8, 2022 21:58
Node.js 8 Cloud Function to write to a Google Sheets document
// Copyright 2018 Google LLC.
// SPDX-License-Identifier: Apache-2.0
const { google } = require("googleapis");
const { Storage } = require("@google-cloud/storage");
exports.csv2sheet = async (data, context) => {
var fileName = data.name;
// basic check that this is a *.csv file, etc...
if (!fileName.endsWith(".csv")) {
@alexismp
alexismp / index.js
Last active June 11, 2019 13:49
Cloud Function csv2sheet - Part 1
const {google} = require("googleapis");
const {Storage} = require("@google-cloud/storage")
exports.csv2sheet = async (data, context) => {
var fileName = data.name;
// basic check that this is a *.csv file, etc...
if (!fileName.endsWith(".csv")) {
console.log("Not a .csv file, ignoring.");
return;
}
@alexismp
alexismp / package.json
Last active June 11, 2019 13:49
csv2sheet dependencies
{
"name": "csv2sheet",
"version": "0.0.42",
"dependencies": {
"googleapis": "^40.0.0",
"@google-cloud/storage": "^2.5.0"
}
}
@alexismp
alexismp / index.js
Last active June 11, 2019 13:48
csv2sheet auth
const auth = await google.auth.getClient({
scopes: [
"https://www.googleapis.com/auth/spreadsheets",
"https://www.googleapis.com/auth/devstorage.read_only"
]
});
@alexismp
alexismp / index.js
Created December 28, 2018 16:05
Sheets API client
const sheetsAPI = google.sheets({version: 'v4', auth});
@alexismp
alexismp / index.js
Last active June 11, 2019 13:48
csv2sheet, addEmptySheet
function addEmptySheet(sheetsAPI, sheetName) {
return new Promise((resolve, reject) => {
const emptySheetParams = {
spreadsheetId: process.env.SPREADSHEET_ID,
resource: {
requests: [
{
addSheet: {
properties: {
title: sheetName,
@alexismp
alexismp / index.js
Last active July 15, 2019 19:52
csv2sheet readCSVContent (assumes CSV content, no parsing)
function readCSVContent(sheetsAPI, file, sheetName) {
return new Promise((resolve, reject) => {
const storage = new Storage();
let fileContents = new Buffer('');
let rows = [];
storage
.bucket(file.bucket)
.file(file.name)
.createReadStream()
.on("error", function(err) {
@alexismp
alexismp / index.js
Last active May 8, 2020 14:45
csv2sheet populateAndStyle
function populateAndStyle(sheetsAPI, theData, sheetId) {
return new Promise((resolve, reject) => {
// Using 'batchUpdate' allows for multiple 'requests' to be sent in a single batch.
// Populate the sheet referenced by its ID with the data received (a CSV string)
// Style: set first row font size to 11 and to Bold. Exercise left for the reader: resize columns
const dataAndStyle = {
spreadsheetId: process.env.SPREADSHEET_ID,
resource: {
requests: [
{
@alexismp
alexismp / index.js
Last active June 11, 2019 13:47
csv2sheet style headers
function setColumnStyle(sheetsAPI, sheetId) {
return new Promise((resolve, reject) => {
const setStyleParams = {
spreadsheetId: process.env.SPREADSHEET_ID,
resource: {
requests: [
{
repeatCell: {
range: {
sheetId: sheetId,
@alexismp
alexismp / index.js
Last active December 27, 2019 03:17
csv2sheet block and call functions
const sheetId = await addEmptySheet(sheetsAPI, sheetName);
const theData = await readCSVContent(sheetsAPI, data, sheetName);
await populateAndStyle(sheetsAPI, theData, sheetId);