Skip to content

Commit 01862ad

Browse files
authored
Merge pull request #127 from brafdlog/sheetsFixes
Sheets fixes
2 parents b1d364f + 1ec2bdb commit 01862ad

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/originalBudgetTrackingApp/export/outputVendors/googleSheets/googleSheets.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ const createTransactionsInGoogleSheets: ExportTransactionsFunction = async (
1616
const { spreadsheetId, credentials } = outputVendorsConfig.googleSheets!.options;
1717
if (!credentials) throw new Error('You must set the \'credentials\'');
1818
const oAuthClient = createClient(credentials);
19+
20+
const sheet = await googleSheets.getSheet(spreadsheetId, sheetName, oAuthClient);
21+
if (!sheet) {
22+
throw new Error(`There is no sheet called ${sheetName} in the spreadsheet`);
23+
}
24+
1925
const hashesAlreadyExistingInGoogleSheets = await googleSheets.getExistingHashes(spreadsheetId, sheetName, oAuthClient);
2026
const transactionsToCreate = transactions.filter((transaction) => !hashesAlreadyExistingInGoogleSheets.includes(transaction.hash));
2127

src/originalBudgetTrackingApp/export/outputVendors/googleSheets/googleSheetsInternalAPI.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export async function appendToSpreadsheet(spreadsheetId: string, range: string,
2525
return result;
2626
}
2727

28-
export async function getExistingHashes(spreadsheetId: string, sheetName: string, auth: OAuth2Client, hashColumn = 'H') {
28+
export async function getExistingHashes(spreadsheetId: string, sheetName: string, auth: OAuth2Client, hashColumn = 'G') {
2929
const result = await sheets.spreadsheets.values.get({
3030
auth,
3131
spreadsheetId,
@@ -44,3 +44,13 @@ export const getAllSpreadsheets = async (auth: OAuth2Client) => {
4444

4545
return response.data.files as Spreadsheet[];
4646
};
47+
48+
export async function getSheet(spreadsheetId: string, sheetName: string, auth: OAuth2Client) {
49+
const spreadsheetResponse = await sheets.spreadsheets.get({
50+
auth,
51+
spreadsheetId
52+
});
53+
const sheet = spreadsheetResponse.data.sheets?.find(({ properties }) => properties?.title === sheetName);
54+
55+
return sheet;
56+
}

0 commit comments

Comments
 (0)