diff --git a/README.md b/README.md index 13e4843..e33090a 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,10 @@ # Array To Google Sheets # [![NPM version][npm-image]][npm-url] -[![build status][travis-image]][travis-url] [![Test coverage][codecov-image]][codecov-url] [npm-image]: https://img.shields.io/npm/v/array-to-google-sheets.svg [npm-url]: https://npmjs.org/package/array-to-google-sheets -[travis-image]: https://img.shields.io/travis/terence410/array-to-google-sheets.svg?style=flat-square -[travis-url]: https://travis-ci.org/terence410/array-to-google-sheets [codecov-image]: https://img.shields.io/codecov/c/github/terence410/array-to-google-sheets.svg?style=flat-square [codecov-url]: https://codecov.io/gh/terence410/array-to-google-sheets diff --git a/src/Sheet.ts b/src/Sheet.ts index b1151a1..c971db9 100644 --- a/src/Sheet.ts +++ b/src/Sheet.ts @@ -191,6 +191,34 @@ export class Sheet { return updateResponse; } + public async updateColumn(columnIndex: number, values: IValues, updateOptions: Partial = {}): Promise { + const options = Object.assign({minRow: 0, minColumn: 0, margin: 2, fitToSize: false, clearAllValues: false}, updateOptions); + + const client = this._getClient(); + const params = { + valueInputOption: options.valueInputOption || "USER_ENTERED", + }; + + const updateResponse: IUpdateResponse = {updatedCells: 0, updatedColumns: 0, updatedRows: 0}; + let body = { values: values }; + const totalRows = values.length; + const range = this._getRange(1, totalRows, columnIndex, 0); + const url = `/${this.spreadsheetId}/values/${range}`; + const res = await client.request({ + baseURL: GOOGLE_SPREADSHEETS_URL, + url, + params, + data: body, + method: "PUT", + }); + + const currentUpdateResponse = res.data as IUpdateResponse; + updateResponse.updatedRows += currentUpdateResponse.updatedRows; + updateResponse.updatedColumns = Math.max(updateResponse.updatedColumns, currentUpdateResponse.updatedColumns); + updateResponse.updatedCells += currentUpdateResponse.updatedCells; + return updateResponse; + } + public async updateRow(rowIndex: number, row: IRow, options: IUpdateBaseOptions = {}): Promise { const client = this._getClient(); const range = this._getRange(row.length, 1, 0, rowIndex); diff --git a/tests/general.test.ts b/tests/general.test.ts index 1a2762f..56c3225 100644 --- a/tests/general.test.ts +++ b/tests/general.test.ts @@ -149,6 +149,27 @@ describe("general", () => { // clean up await sheet.delete(); }); + + it("updateColumn", async () => { + const spreadsheet = await googleSheets.getSpreadsheet(spreadsheetId); + const sheet = await spreadsheet.findOrCreateSheet(sheetName); + + // resize it first + await sheet.clear(); + await sheet.resize(10, 10); + + // add column + const total = 5; + const values1: number[][] = []; // Initialize values1 as an empty array + for (let i = 0; i < total; i++) { + values1.push([i]); + await sheet.updateColumn(0, values1); + } + + const finalValues = await sheet.getValues(); + assert.equal(finalValues.length, total); + assert.deepEqual(finalValues, values1); + }); it("updateRow", async () => { const spreadsheet = await googleSheets.getSpreadsheet(spreadsheetId);