From 29daa72ef18893dfe73fb58a6c4c5b9a2feb84b8 Mon Sep 17 00:00:00 2001 From: Benjamin Russell Date: Wed, 5 Oct 2022 11:40:00 -0500 Subject: [PATCH] Give saveAsExcel its own config options (#20647) Co-authored-by: Ben Russell --- src/sql/platform/query/common/query.ts | 3 ++ .../browser/outputs/gridOutput.component.ts | 20 +++++++--- .../query/browser/query.contribution.ts | 5 +++ .../services/query/common/resultSerializer.ts | 37 ++++++++++++------- 4 files changed, 46 insertions(+), 19 deletions(-) diff --git a/src/sql/platform/query/common/query.ts b/src/sql/platform/query/common/query.ts index f33c0dee41..5f0a0d0ff7 100644 --- a/src/sql/platform/query/common/query.ts +++ b/src/sql/platform/query/common/query.ts @@ -12,6 +12,9 @@ export interface IQueryEditorConfiguration { readonly textIdentifier: string, readonly encoding: string }, + readonly saveAsExcel: { + readonly includeHeaders: boolean, + }, readonly saveAsXml: { readonly formatted: boolean, readonly encoding: string diff --git a/src/sql/workbench/contrib/notebook/browser/outputs/gridOutput.component.ts b/src/sql/workbench/contrib/notebook/browser/outputs/gridOutput.component.ts index 43699dcc8c..31230226fe 100644 --- a/src/sql/workbench/contrib/notebook/browser/outputs/gridOutput.component.ts +++ b/src/sql/workbench/contrib/notebook/browser/outputs/gridOutput.component.ts @@ -438,7 +438,7 @@ export class DataResourceDataProvider implements IGridDataProvider { if (!this.canSerialize) { return Promise.resolve(undefined); } - // TODO implement selection support + let columns = this._resultSet.columnInfo; let rowLength = this._rows.length; let minRow = 0; @@ -482,13 +482,21 @@ export class DataResourceDataProvider implements IGridDataProvider { return result; }; - let serializeRequestParams: SerializeDataParams = Object.assign(serializer.getBasicSaveParameters(format), >{ + // This code path uses the serialization service which uses a different request parameter + // interface than the query execution service's saveResults handlers. Here, we take the + // format-specific request params (eg, includeHeaders for CSV) and merge the format-agnostic + // request params for the serialization request (eg, saveFormat, filePath). + let formatSpecificParams = serializer.getBasicSaveParameters(format); + let formatAgnosticParams = >{ saveFormat: format, - columns: columns, filePath: filePath.fsPath, - getRowRange: (rowStart, includeHeaders, numberOfRows) => getRows(rowStart, includeHeaders, numberOfRows), - rowCount: rowLength - }); + columns: columns, + rowCount: rowLength, + getRowRange: (rowStart: number, includeHeaders: boolean, numberOfRows?: number) => + getRows(rowStart, includeHeaders, numberOfRows), + }; + let serializeRequestParams = Object.assign(formatSpecificParams, formatAgnosticParams); + return this._serializationService.serializeResults(serializeRequestParams); } diff --git a/src/sql/workbench/contrib/query/browser/query.contribution.ts b/src/sql/workbench/contrib/query/browser/query.contribution.ts index f2ffaa388e..73b2289ece 100644 --- a/src/sql/workbench/contrib/query/browser/query.contribution.ts +++ b/src/sql/workbench/contrib/query/browser/query.contribution.ts @@ -374,6 +374,11 @@ const queryEditorConfiguration: IConfigurationNode = { 'description': localize('queryEditor.results.saveAsCsv.textIdentifier', "Character used for enclosing text fields when saving results as CSV"), 'default': '\"' }, + 'queryEditor.results.saveAsExcel.includeHeaders': { + 'type': 'boolean', + 'description': localize('queryEditor.results.saveAsExcel.includeHeaders', "When true, column headers are included when saving results as an Excel file"), + 'default': true + }, 'queryEditor.results.saveAsCsv.encoding': { 'type': 'string', 'description': localize('queryEditor.results.saveAsCsv.encoding', "File encoding used when saving results as CSV"), diff --git a/src/sql/workbench/services/query/common/resultSerializer.ts b/src/sql/workbench/services/query/common/resultSerializer.ts index 4c0247a886..18e9429dfb 100644 --- a/src/sql/workbench/services/query/common/resultSerializer.ts +++ b/src/sql/workbench/services/query/common/resultSerializer.ts @@ -191,6 +191,7 @@ export class ResultSerializer { // get save results config from vscode config let saveConfig = this._configurationService.getValue('queryEditor').results.saveAsCsv; + // if user entered config, set options if (saveConfig) { if (saveConfig.includeHeaders !== undefined) { @@ -215,21 +216,23 @@ export class ResultSerializer { private getConfigForJson(): SaveResultsRequestParams { // JSON does not currently have special conditions - let saveResultsParams = { resultFormat: SaveFormat.JSON as string }; - return saveResultsParams; + return { resultFormat: SaveFormat.JSON as string }; } private getConfigForExcel(): SaveResultsRequestParams { - // get save results config from vscode config - // Note: we are currently using the configSaveAsCsv setting since it has the option mssql.saveAsCsv.includeHeaders - // and we want to have just 1 setting that lists this. - let config = this.getConfigForCsv(); - config.resultFormat = SaveFormat.EXCEL; - config.delimiter = undefined; - config.lineSeperator = undefined; - config.textIdentifier = undefined; - config.encoding = undefined; - return config; + let saveResultsParams = { resultFormat: SaveFormat.EXCEL as string }; + + // Get save results config from vscode config + let saveConfig = this._configurationService.getValue('queryEditor').results.saveAsExcel; + + // If user entered config, set options + if (saveConfig) { + if (saveConfig.includeHeaders !== undefined) { + saveResultsParams.includeHeaders = saveConfig.includeHeaders; + } + } + + return saveResultsParams; } private getConfigForXml(): SaveResultsRequestParams { @@ -237,6 +240,7 @@ export class ResultSerializer { // get save results config from vscode config let saveConfig = this._configurationService.getValue('queryEditor').results.saveAsXml; + // if user entered config, set options if (saveConfig) { if (saveConfig.formatted !== undefined) { @@ -251,7 +255,14 @@ export class ResultSerializer { } - private getParameters(uri: string, filePath: URI, batchIndex: number, resultSetNo: number, format: string, selection?: Slick.Range): SaveResultsRequestParams { + private getParameters( + uri: string, + filePath: URI, + batchIndex: number, + resultSetNo: number, + format: string, + selection?: Slick.Range + ): SaveResultsRequestParams { let saveResultsParams = this.getBasicSaveParameters(format); saveResultsParams.filePath = filePath.fsPath; saveResultsParams.ownerUri = uri;