diff --git a/src/exportToSheets.ts b/src/exportToSheets.ts index 54df4083..8ce77367 100644 --- a/src/exportToSheets.ts +++ b/src/exportToSheets.ts @@ -15,6 +15,7 @@ import * as google from '@googleapis/sheets'; import {getIssues} from './issue'; import {Issue} from './types'; +import {NUMBER_TO_DELETE} from './util'; const spreadsheetId = '1VV5Clqstgoeu1qVwpbKkYOxwEgjvhMhSkVCBLMqg24M'; export const fixtures = { @@ -75,14 +76,8 @@ export async function exportToSheets() { version: 'v4', auth: await fixtures.getClient(), }); - // clear the current text in the sheet except for the field labels - // on the first row. - await sheets.spreadsheets.values.clear({ - spreadsheetId, - range: 'A2:Z10000', - }); - // insert it into the sheet + // first update the data into the sheet await sheets.spreadsheets.values.batchUpdate({ spreadsheetId, requestBody: { @@ -95,4 +90,13 @@ export async function exportToSheets() { ], }, }); + + // then clear the excess data + const start = values.length + 1; + const end = start + NUMBER_TO_DELETE; + + await sheets.spreadsheets.values.clear({ + spreadsheetId, + range: `A${start}:Z${end}`, + }); } diff --git a/src/fetchServices.ts b/src/fetchServices.ts index caf82967..5948516d 100644 --- a/src/fetchServices.ts +++ b/src/fetchServices.ts @@ -21,6 +21,7 @@ import * as meow from 'meow'; import Table = require('cli-table'); import {allow, deny} from './services.json'; import * as CSV from 'csv-string'; +import {NUMBER_TO_DELETE} from './util'; const auth = new google.auth.GoogleAuth({ scopes: [ @@ -167,14 +168,7 @@ export async function exportApisToSheets() { const values = await getResults(); values.unshift(['Service', 'Title', 'Group', 'HasSurface', 'InScope', 'ToS']); - // clear the current text in the sheet except for the field labels - // on the first row. - await sheets.spreadsheets.values.clear({ - spreadsheetId, - range: 'all_apis!A2:Z10000', - }); - - // insert it into the sheet + // first update the data into the sheet await sheets.spreadsheets.values.batchUpdate({ spreadsheetId: spreadsheetId, requestBody: { @@ -187,6 +181,15 @@ export async function exportApisToSheets() { ], }, }); + + // then clear the excess data + const start = values.length + 1; + const end = start + NUMBER_TO_DELETE; + + await sheets.spreadsheets.values.clear({ + spreadsheetId, + range: `all_apis!A${start}:Z${end}`, + }); } /** diff --git a/src/util.ts b/src/util.ts index 6cf3439f..902e5858 100644 --- a/src/util.ts +++ b/src/util.ts @@ -27,3 +27,5 @@ export async function gethub(options: GaxiosOptions) { }; return request(options); } + +export const NUMBER_TO_DELETE = 10000; diff --git a/test/test.exportToSheets.ts b/test/test.exportToSheets.ts index 8a809da4..228672fb 100644 --- a/test/test.exportToSheets.ts +++ b/test/test.exportToSheets.ts @@ -24,6 +24,7 @@ process.env.DRIFT_API_KEY = 'not-a-key'; import {exportToSheets, fixtures} from '../src/exportToSheets'; import * as issues from '../src/issue'; +import {NUMBER_TO_DELETE} from '../src/util'; nock.disableNetConnect(); const sandbox = sinon.createSandbox(); @@ -70,10 +71,14 @@ describe('exportToSheets', () => { sandbox.stub(fixtures, 'getClient').resolves(jwt); const sheetPath = '/v4/spreadsheets/1VV5Clqstgoeu1qVwpbKkYOxwEgjvhMhSkVCBLMqg24M'; + + // The test data has one row plus the file name row. + const start = 3; + const end = start + NUMBER_TO_DELETE; const scope = nock('https://sheets.googleapis.com') - .post(`${sheetPath}/values/A2%3AZ10000:clear`) - .reply(200) .post(`${sheetPath}/values:batchUpdate`) + .reply(200) + .post(`${sheetPath}/values/A${start}%3AZ${end}:clear`) .reply(200); await exportToSheets(); scope.done();