Skip to content

Commit 3b24e6d

Browse files
committed
clean up setDataValidation
1 parent b9e8720 commit 3b24e6d

File tree

6 files changed

+82
-35
lines changed

6 files changed

+82
-35
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Full docs available at [https://theoephraim.github.io/node-google-spreadsheet](h
2525

2626
_The following examples are meant to give you an idea of just some of the things you can do_
2727

28-
> **IMPORTANT NOTE** - To keep the examples concise, I'm calling await [at the top level](https://v8.dev/features/top-level-await) which is not allowed by default in most versions of node. If you need to call await in a script at the root level, you must instead wrap it in an async function like so:
28+
> **IMPORTANT NOTE** - To keep the examples concise, I'm calling await [at the top level](https://v8.dev/features/top-level-await) which is not allowed in some older versions of node. If you need to call await in a script at the root level and your environment does not support it, you must instead wrap it in an async function like so:
2929
3030
```javascript
3131
(async function () {

docs/classes/google-spreadsheet-worksheet.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,24 @@ Param|Type|Required|Description
333333

334334
?> The authentication method being used must have write access to the destination document as well
335335

336+
337+
#### `setDataValidation(range, rule)` (async) :id=fn-setDataValidation
338+
> Sets a data validation rule to every cell in the range
339+
340+
Param|Type|Required|Description
341+
---|---|---|---
342+
`range`|Object<br>[GridRange](https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/other#GridRange)|✅|Range of cells to apply the rule to, sheetId not required!
343+
`rule`|Object<br>[DataValidationRule](https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/cells#DataValidationRule)<br>or `false`|✅|Object describing the validation rule<br/>Or `false` to unset the rule
344+
345+
346+
-**Side Effects -** sheet is copied to the other doc
347+
348+
?> The authentication method being used must have write access to the destination document as well
349+
350+
351+
352+
353+
336354
### Exports
337355

338356
See [Exports guide](guides/exports) for more info.

pnpm-lock.yaml

Lines changed: 16 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/lib/GoogleSpreadsheetWorksheet.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import { GoogleSpreadsheet } from './GoogleSpreadsheet';
1111
import {
1212
A1Range, SpreadsheetId, DimensionRangeIndexes, WorksheetDimension, WorksheetId, WorksheetProperties, A1Address,
1313
RowIndex, ColumnIndex, DataFilterWithoutWorksheetId, DataFilter, GetValuesRequestOptions, WorksheetGridProperties,
14-
WorksheetDimensionProperties, CellDataRange, AddRowOptions, GridRangeWithOptionalWorksheetId, GridRange, DataValidationRule,
14+
WorksheetDimensionProperties, CellDataRange, AddRowOptions, GridRangeWithOptionalWorksheetId,
15+
DataValidationRule,
1516
} from './types/sheets-types';
1617

1718

@@ -820,15 +821,20 @@ export class GoogleSpreadsheetWorksheet {
820821
}
821822

822823
/**
824+
* Sets (or unsets) a data validation rule to every cell in the range
823825
* @see https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/request#SetDataValidationRequest
824826
*/
825-
async setDataValidation({ range, rule }:{ range: Omit<GridRange, 'sheetId'>, rule: DataValidationRule }) {
827+
async setDataValidation(
828+
range: GridRangeWithOptionalWorksheetId,
829+
/** data validation rule object, or set to false to clear an existing rule */
830+
rule: DataValidationRule | false
831+
) {
826832
return this._makeSingleUpdateRequest('setDataValidation', {
827833
range: {
828834
sheetId: this.sheetId,
829835
...range,
830836
},
831-
rule
837+
...rule && { rule },
832838
});
833839
}
834840

src/lib/types/sheets-types.ts

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -586,16 +586,14 @@ export type ConditionValue =
586586
* @see https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/other#BooleanCondition
587587
*/
588588
export type BooleanCondition = {
589-
/**
590-
* The type of condition.
591-
*/
589+
/** The type of condition. */
592590
type: ConditionType;
593-
594591
/**
595-
* The values of the condition. The number of supported values depends on the condition type. Some support zero values, others one or two values, and ConditionType.ONE_OF_LIST supports an arbitrary number of values.
592+
* The values of the condition.
593+
* The number of supported values depends on the condition type. Some support zero values, others one or two values, and ConditionType.ONE_OF_LIST supports an arbitrary number of values.
596594
*/
597595
values: ConditionValue[];
598-
}
596+
};
599597

600598
/**
601599
* @see https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/cells#DataValidationRule
@@ -604,23 +602,12 @@ export type BooleanCondition = {
604602
* - https://stackoverflow.com/a/43442775/3068233
605603
*/
606604
export type DataValidationRule = {
607-
/**
608-
* The condition that data in the cell must match.
609-
*/
605+
/** The condition that data in the cell must match. */
610606
condition: BooleanCondition;
611-
612-
/**
613-
* A message to show the user when adding data to the cell.
614-
*/
607+
/** A message to show the user when adding data to the cell. */
615608
inputMessage?: string;
616-
617-
/**
618-
* True if invalid data should be rejected.
619-
*/
609+
/** True if invalid data should be rejected. */
620610
strict: boolean;
621-
622-
/**
623-
* True if the UI should be customized based on the kind of condition. If true, "List" conditions will show a dropdown.
624-
*/
611+
/** True if the UI should be customized based on the kind of condition. If true, "List" conditions will show a dropdown. */
625612
showCustomUi: boolean;
626-
}
613+
};

src/test/manage.test.ts

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -188,17 +188,29 @@ describe('Managing doc info and sheets', () => {
188188
await sheet.loadCells();
189189
expect(sheet.cellStats.nonEmpty).toBe(0);
190190
});
191+
});
192+
193+
describe.only('data validation rules', () => {
194+
let sheet: GoogleSpreadsheetWorksheet;
195+
196+
beforeAll(async () => {
197+
sheet = await doc.addSheet({ title: 'validation rules test' });
198+
});
199+
afterAll(async () => {
200+
await sheet.delete();
201+
});
202+
191203

192204
it('can set data validation', async () => {
193205
// add a dropdown; ref: https://stackoverflow.com/a/43442775/3068233
194-
await sheet.setDataValidation({
195-
range: {
206+
await sheet.setDataValidation(
207+
{
196208
startRowIndex: 2,
197209
endRowIndex: 100,
198210
startColumnIndex: 3,
199211
endColumnIndex: 4,
200212
},
201-
rule: {
213+
{
202214
condition: {
203215
type: 'ONE_OF_LIST',
204216
values: [
@@ -215,9 +227,21 @@ describe('Managing doc info and sheets', () => {
215227
},
216228
showCustomUi: true,
217229
strict: true,
230+
}
231+
);
232+
});
233+
234+
it('can clear a data validation', async () => {
235+
await sheet.setDataValidation(
236+
{
237+
startRowIndex: 2,
238+
endRowIndex: 100,
239+
startColumnIndex: 3,
240+
endColumnIndex: 4,
218241
},
219-
})
220-
})
242+
false
243+
);
244+
});
221245
});
222246

223247
describe('deleting a sheet', () => {

0 commit comments

Comments
 (0)