-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsheets-format.js
More file actions
174 lines (145 loc) · 5.39 KB
/
sheets-format.js
File metadata and controls
174 lines (145 loc) · 5.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import { getSheetsClient } from './google-clients.js';
import { parseColor, parseA1Range, colToNum } from './sheets-core.js';
export async function formatRange(auth, sheetId, range, formatting) {
const sheets = getSheetsClient(auth);
const parsed = parseA1Range(range);
if (!parsed) throw new Error(`Invalid range format: ${range}`);
const spreadsheet = await sheets.spreadsheets.get({ spreadsheetId: sheetId });
let targetSheet = spreadsheet.data.sheets[0];
if (parsed.sheetName) {
targetSheet = spreadsheet.data.sheets.find(s => s.properties.title === parsed.sheetName);
if (!targetSheet) throw new Error(`Sheet tab not found: ${parsed.sheetName}`);
}
const cellFormat = {};
const fields = [];
if (formatting.backgroundColor) {
const color = parseColor(formatting.backgroundColor);
if (color) {
cellFormat.backgroundColor = color;
fields.push('userEnteredFormat.backgroundColor');
}
}
if (formatting.textColor) {
const color = parseColor(formatting.textColor);
if (color) {
cellFormat.textFormat = cellFormat.textFormat || {};
cellFormat.textFormat.foregroundColor = color;
fields.push('userEnteredFormat.textFormat.foregroundColor');
}
}
if (formatting.bold !== undefined) {
cellFormat.textFormat = cellFormat.textFormat || {};
cellFormat.textFormat.bold = formatting.bold;
fields.push('userEnteredFormat.textFormat.bold');
}
if (formatting.italic !== undefined) {
cellFormat.textFormat = cellFormat.textFormat || {};
cellFormat.textFormat.italic = formatting.italic;
fields.push('userEnteredFormat.textFormat.italic');
}
if (formatting.fontSize) {
cellFormat.textFormat = cellFormat.textFormat || {};
cellFormat.textFormat.fontSize = formatting.fontSize;
fields.push('userEnteredFormat.textFormat.fontSize');
}
if (formatting.fontFamily) {
cellFormat.textFormat = cellFormat.textFormat || {};
cellFormat.textFormat.fontFamily = formatting.fontFamily;
fields.push('userEnteredFormat.textFormat.fontFamily');
}
if (formatting.horizontalAlignment) {
cellFormat.horizontalAlignment = formatting.horizontalAlignment.toUpperCase();
fields.push('userEnteredFormat.horizontalAlignment');
}
if (formatting.verticalAlignment) {
cellFormat.verticalAlignment = formatting.verticalAlignment.toUpperCase();
fields.push('userEnteredFormat.verticalAlignment');
}
if (formatting.wrapStrategy) {
cellFormat.wrapStrategy = formatting.wrapStrategy.toUpperCase();
fields.push('userEnteredFormat.wrapStrategy');
}
if (formatting.numberFormat) {
cellFormat.numberFormat = {
type: formatting.numberFormat.type || 'NUMBER',
pattern: formatting.numberFormat.pattern || ''
};
fields.push('userEnteredFormat.numberFormat');
}
const requests = [];
if (fields.length > 0) {
requests.push({
repeatCell: {
range: {
sheetId: targetSheet.properties.sheetId,
startRowIndex: parsed.startRow,
endRowIndex: parsed.endRow + 1,
startColumnIndex: parsed.startCol,
endColumnIndex: parsed.endCol + 1
},
cell: { userEnteredFormat: cellFormat },
fields: fields.join(',')
}
});
}
if (formatting.borders) {
const borderStyle = {
style: formatting.borders.style || 'SOLID',
color: parseColor(formatting.borders.color) || { red: 0, green: 0, blue: 0 }
};
requests.push({
updateBorders: {
range: {
sheetId: targetSheet.properties.sheetId,
startRowIndex: parsed.startRow,
endRowIndex: parsed.endRow + 1,
startColumnIndex: parsed.startCol,
endColumnIndex: parsed.endCol + 1
},
top: borderStyle,
bottom: borderStyle,
left: borderStyle,
right: borderStyle,
innerHorizontal: formatting.borders.inner ? borderStyle : undefined,
innerVertical: formatting.borders.inner ? borderStyle : undefined
}
});
}
if (requests.length === 0) {
throw new Error('No formatting options specified.');
}
await sheets.spreadsheets.batchUpdate({
spreadsheetId: sheetId,
requestBody: { requests }
});
return { formatted: range };
}
export async function mergeCells(auth, sheetId, range, action = 'merge') {
const sheets = getSheetsClient(auth);
const parsed = parseA1Range(range);
if (!parsed) throw new Error(`Invalid range format: ${range}`);
const spreadsheet = await sheets.spreadsheets.get({ spreadsheetId: sheetId });
let targetSheet = spreadsheet.data.sheets[0];
if (parsed.sheetName) {
targetSheet = spreadsheet.data.sheets.find(s => s.properties.title === parsed.sheetName);
if (!targetSheet) throw new Error(`Sheet tab not found: ${parsed.sheetName}`);
}
const rangeObj = {
sheetId: targetSheet.properties.sheetId,
startRowIndex: parsed.startRow,
endRowIndex: parsed.endRow + 1,
startColumnIndex: parsed.startCol,
endColumnIndex: parsed.endCol + 1
};
const request = action === 'unmerge'
? { unmergeCells: { range: rangeObj } }
: { mergeCells: { range: rangeObj, mergeType: 'MERGE_ALL' } };
await sheets.spreadsheets.batchUpdate({
spreadsheetId: sheetId,
requestBody: { requests: [request] }
});
return action === 'unmerge' ? { unmerged: range } : { merged: range };
}
export async function unmergeCells(auth, sheetId, range) {
return mergeCells(auth, sheetId, range, 'unmerge');
}