From b897fd5e5c1f2b953e66e63d23f12ba2485a2a0e Mon Sep 17 00:00:00 2001 From: martinshoob Date: Wed, 28 May 2025 09:56:51 +0200 Subject: [PATCH] Added function to auto-adjust column width --- app.js | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/app.js b/app.js index d39ff95..5a711e7 100644 --- a/app.js +++ b/app.js @@ -102,6 +102,37 @@ async function resetTableData() { } } +async function autoResizeColumns(spreadsheetId, sheetId, startColumnIndex, endColumnIndex) { + try { + const sheets = await createSheetsClient(); + + const request = { + spreadsheetId: spreadsheetId, + resource: { + requests: [ + { + autoResizeDimensions: { + dimensions: { + sheetId: sheetId, + dimension: 'COLUMNS', + startIndex: startColumnIndex, + endIndex: endColumnIndex + 1 + } + } + } + ] + } + }; + + const response = await sheets.spreadsheets.batchUpdate(request); + console.log('Columns auto-resized successfully'); + return response.data; + } catch (err) { + console.error('Error auto-resizing columns:', err); + throw err; + } +} + async function main() { const splidCodes = await getSheetData(SPREADSHEET_ID, SHEET_RANGES.SPLID_CODES); const userTags = await getSheetData(SPREADSHEET_ID, SHEET_RANGES.USER_TAGS); @@ -131,6 +162,14 @@ async function main() { // Update the sheet with the row data await updateSheetData(SPREADSHEET_ID, SHEET_RANGES.DATA_ROW(i), [rowData]); } + + console.log("Sheets updated, adjusting column width.") + + try { + await autoResizeColumns(SPREADSHEET_ID, SHEET_ID, COLUMN_INDICES.E, COLUMN_INDICES.M); + } catch (err) { + console.error('Failed to auto-resize columns, but data was updated successfully:', err); + } } -main().catch(console.error); \ No newline at end of file +main().catch(console.error);