import { drive_v3 as driveV3 , google } from 'googleapis' ; import _ from 'lodash' ; import { OAuth2Client } from './googleAuth' ; const sheets = google . sheets ( { version : 'v4' } ) ; const drive = google . drive ( { version : 'v3' } ) ; see https param { google . auth . OAuth2 } auth export async function appendToSpreadsheet ( spreadsheetId , range , values , auth ) { const requestBody = { values } ; const result = await sheets . spreadsheets . values . append ( { auth , spreadsheetId , range , valueInputOption : 'USER_ENTERED' , requestBody } ) ; return result ; } export async function getExistingHashes ( spreadsheetId , sheetName , auth , hashColumn = 'G' ) { const result = await sheets . spreadsheets . values . get ( { auth , spreadsheetId , range : `${ sheetName }!${ hashColumn }:${ hashColumn }` } ) ; const existingHashes = _ . flatten ( result . data . values ) ; return existingHashes ; } export type Spreadsheet = Pick < driveV3 . Schema$File , 'id' | 'name' > ; export const getAllSpreadsheets = async ( auth ) => { const response = await drive . files . list ( { q : 'mimeType="application/vnd.google-apps.spreadsheet"' , auth } ) ; return response . data . files as Spreadsheet [ ] ; } ; export async function getSheet ( spreadsheetId , sheetName , auth ) { const spreadsheetResponse = await sheets . spreadsheets . get ( { auth , spreadsheetId } ) ; const sheet = spreadsheetResponse . data . sheets ?. find ( ( { properties } ) => properties ?. title === sheetName ) ; return sheet ; } export async function createSpreadsheet ( spreadsheetTitle , sheetTitle , auth ) { const spreadsheetResponse = await sheets . spreadsheets . create ( { auth , requestBody : { properties : { title : spreadsheetTitle } , sheets : [ { properties : { title : sheetTitle } } ] } } ) ; const { spreadsheetId } = spreadsheetResponse . data ; return spreadsheetId as string ; }