Skip to content

Commit

Permalink
feat(facade): add setFontColor
Browse files Browse the repository at this point in the history
  • Loading branch information
hexf00 committed Jan 26, 2024
1 parent 3995c7c commit f9cd97d
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
34 changes: 34 additions & 0 deletions packages/facade/src/apis/sheet/__tests__/f-range.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,4 +375,38 @@ describe('Test FRange', () => {
expect(getStyleByPosition(1, 0, 1, 0)?.fs).toBe(undefined);
expect(getStyleByPosition(1, 1, 1, 1)?.fs).toBe(undefined);
});

it('Range setFontColor', () => {
const activeSheet = univerAPI.getActiveWorkbook()?.getActiveSheet();

// change A1 Font Color
const range = activeSheet?.getRange(0, 0);
expect(getStyleByPosition(0, 0, 0, 0)?.cl).toBe(undefined);
range?.setFontColor('red');
expect(getStyleByPosition(0, 0, 0, 0)?.cl?.rgb).toBe('red');
range?.setFontColor('green');
expect(getStyleByPosition(0, 0, 0, 0)?.cl?.rgb).toBe('green');
range?.setFontColor(null);
expect(getStyleByPosition(0, 0, 0, 0)?.cl).toBe(undefined);

// change A1:B2 Font Color
const range2 = activeSheet?.getRange(0, 0, 2, 2);
range2?.setFontColor('red');
expect(getStyleByPosition(0, 0, 0, 0)?.cl?.rgb).toBe('red');
expect(getStyleByPosition(0, 1, 0, 1)?.cl?.rgb).toBe('red');
expect(getStyleByPosition(1, 0, 1, 0)?.cl?.rgb).toBe('red');
expect(getStyleByPosition(1, 1, 1, 1)?.cl?.rgb).toBe('red');

range2?.setFontColor('green');
expect(getStyleByPosition(0, 0, 0, 0)?.cl?.rgb).toBe('green');
expect(getStyleByPosition(0, 1, 0, 1)?.cl?.rgb).toBe('green');
expect(getStyleByPosition(1, 0, 1, 0)?.cl?.rgb).toBe('green');
expect(getStyleByPosition(1, 1, 1, 1)?.cl?.rgb).toBe('green');

range2?.setFontColor(null);
expect(getStyleByPosition(0, 0, 0, 0)?.cl).toBe(undefined);
expect(getStyleByPosition(0, 1, 0, 1)?.cl).toBe(undefined);
expect(getStyleByPosition(1, 0, 1, 0)?.cl).toBe(undefined);
expect(getStyleByPosition(1, 1, 1, 1)?.cl).toBe(undefined);
});
});
33 changes: 33 additions & 0 deletions packages/facade/src/apis/sheet/f-range.ts
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,39 @@ export class FRange {
return this;
}

/**
* Sets the font color in CSS notation (such as '#ffffff' or 'white').
* @param color The font color in CSS notation (such as '#ffffff' or 'white'); a null value resets the color.
*/
setFontColor(color: string | null): this {
let style: IStyleTypeValue<IColorStyle | null>;

if (color === null) {
style = {
type: 'cl',
value: null,
};
} else {
style = {
type: 'cl',
value: {
rgb: color,
},
};
}

const setStyleParams: ISetStyleCommandParams<IColorStyle | null> = {
unitId: this._workbook.getUnitId(),
subUnitId: this._worksheet.getSheetId(),
range: this._range,
style,
};

this._commandService.executeCommand(SetStyleCommand.id, setStyleParams);

return this;
}

// #endregion editing

/**
Expand Down

0 comments on commit f9cd97d

Please sign in to comment.