Last active
May 8, 2020 14:45
-
-
Save alexismp/1605c489e41aae3756b0e9ac02f1e4a1 to your computer and use it in GitHub Desktop.
csv2sheet populateAndStyle
This file contains hidden or 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
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: [ | |
{ | |
pasteData: { | |
coordinate: { | |
sheetId: sheetId, | |
rowIndex: 0, | |
columnIndex: 0 | |
}, | |
data: theData, | |
delimiter: "," | |
} | |
}, | |
{ | |
repeatCell: { | |
range: { | |
sheetId: sheetId, | |
startRowIndex: 0, | |
endRowIndex: 1 | |
}, | |
cell: { | |
userEnteredFormat: { | |
textFormat: { | |
fontSize: 11, | |
bold: true | |
} | |
} | |
}, | |
fields: "userEnteredFormat(textFormat)" | |
} | |
} | |
] | |
} | |
}; | |
sheetsAPI.spreadsheets.batchUpdate(dataAndStyle, function(err, response) { | |
if (err) { | |
reject("The Sheets API returned an error: " + err); | |
} else { | |
console.log(sheetId + " sheet populated with " + theData.length + " rows and column style set."); | |
resolve(); | |
} | |
}); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment