2019-06-23 03:35:23 -07:00
|
|
|
import { IDataObject } from 'n8n-workflow';
|
2019-11-30 14:55:22 -08:00
|
|
|
import { google, sheets_v4 } from 'googleapis';
|
2019-06-23 03:35:23 -07:00
|
|
|
import { JWT } from 'google-auth-library';
|
2019-10-27 13:44:21 -07:00
|
|
|
import { getAuthenticationClient } from './GoogleApi';
|
2019-06-23 03:35:23 -07:00
|
|
|
|
|
|
|
const Sheets = google.sheets('v4'); // tslint:disable-line:variable-name
|
|
|
|
|
|
|
|
export interface ISheetOptions {
|
|
|
|
scope: string[];
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface IGoogleAuthCredentials {
|
|
|
|
email: string;
|
|
|
|
privateKey: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface ISheetUpdateData {
|
|
|
|
range: string;
|
|
|
|
values: string[][];
|
|
|
|
}
|
|
|
|
|
2019-08-27 11:27:00 -07:00
|
|
|
export interface ILookupValues {
|
|
|
|
lookupColumn: string;
|
|
|
|
lookupValue: string;
|
|
|
|
}
|
2019-06-23 03:35:23 -07:00
|
|
|
|
2019-11-30 14:55:22 -08:00
|
|
|
export interface IToDeleteRange {
|
|
|
|
amount: number;
|
|
|
|
startIndex: number;
|
|
|
|
sheetId: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface IToDelete {
|
|
|
|
[key: string]: IToDeleteRange[] | undefined;
|
|
|
|
columns?: IToDeleteRange[];
|
|
|
|
rows?: IToDeleteRange[];
|
|
|
|
}
|
|
|
|
|
2019-10-20 12:21:55 -07:00
|
|
|
export type ValueInputOption = 'RAW' | 'USER_ENTERED';
|
|
|
|
|
|
|
|
export type ValueRenderOption = 'FORMATTED_VALUE' | 'FORMULA' | 'UNFORMATTED_VALUE';
|
|
|
|
|
2019-06-23 03:35:23 -07:00
|
|
|
export class GoogleSheet {
|
|
|
|
id: string;
|
|
|
|
credentials: IGoogleAuthCredentials;
|
|
|
|
scopes: string[];
|
|
|
|
|
|
|
|
constructor(spreadsheetId: string, credentials: IGoogleAuthCredentials, options?: ISheetOptions | undefined) {
|
|
|
|
// options = <SheetOptions>options || {};
|
|
|
|
if (!options) {
|
|
|
|
options = {} as ISheetOptions;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.id = spreadsheetId;
|
|
|
|
this.credentials = credentials;
|
|
|
|
this.scopes = options.scope || ['https://www.googleapis.com/auth/spreadsheets'];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-11-02 14:29:52 -07:00
|
|
|
/**
|
|
|
|
* Clears values from a sheet
|
|
|
|
*
|
|
|
|
* @param {string} range
|
|
|
|
* @returns {Promise<object>}
|
|
|
|
* @memberof GoogleSheet
|
|
|
|
*/
|
|
|
|
async clearData(range: string): Promise<object> {
|
|
|
|
const client = await this.getAuthenticationClient();
|
|
|
|
|
|
|
|
const response = await Sheets.spreadsheets.values.clear(
|
|
|
|
{
|
|
|
|
auth: client,
|
|
|
|
spreadsheetId: this.id,
|
|
|
|
range,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
return response.data;
|
|
|
|
}
|
|
|
|
|
2019-06-23 03:35:23 -07:00
|
|
|
/**
|
|
|
|
* Returns the cell values
|
|
|
|
*/
|
2019-10-20 12:21:55 -07:00
|
|
|
async getData(range: string, valueRenderMode: ValueRenderOption): Promise<string[][] | undefined> {
|
2019-06-23 03:35:23 -07:00
|
|
|
const client = await this.getAuthenticationClient();
|
|
|
|
|
|
|
|
const response = await Sheets.spreadsheets.values.get(
|
|
|
|
{
|
|
|
|
auth: client,
|
|
|
|
spreadsheetId: this.id,
|
|
|
|
range,
|
2019-10-20 12:21:55 -07:00
|
|
|
valueRenderOption: valueRenderMode,
|
2019-06-23 03:35:23 -07:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2019-12-30 11:38:55 -08:00
|
|
|
return response.data.values as string[][] | undefined;
|
2019-06-23 03:35:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-11-30 14:55:22 -08:00
|
|
|
/**
|
|
|
|
* Returns the sheets in a Spreadsheet
|
|
|
|
*/
|
|
|
|
async spreadsheetGetSheets() {
|
|
|
|
const client = await this.getAuthenticationClient();
|
|
|
|
|
|
|
|
const response = await Sheets.spreadsheets.get(
|
|
|
|
{
|
|
|
|
auth: client,
|
|
|
|
spreadsheetId: this.id,
|
|
|
|
fields: 'sheets.properties'
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
return response.data;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets values in one or more ranges of a spreadsheet.
|
|
|
|
*/
|
|
|
|
async spreadsheetBatchUpdate(requests: sheets_v4.Schema$Request[]) { // tslint:disable-line:no-any
|
|
|
|
const client = await this.getAuthenticationClient();
|
|
|
|
|
|
|
|
const response = await Sheets.spreadsheets.batchUpdate(
|
|
|
|
{
|
|
|
|
auth: client,
|
|
|
|
spreadsheetId: this.id,
|
|
|
|
requestBody: {
|
|
|
|
requests,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
return response.data;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-06-23 03:35:23 -07:00
|
|
|
/**
|
|
|
|
* Sets the cell values
|
|
|
|
*/
|
2019-10-20 12:21:55 -07:00
|
|
|
async batchUpdate(updateData: ISheetUpdateData[], valueInputMode: ValueInputOption) {
|
2019-06-23 03:35:23 -07:00
|
|
|
const client = await this.getAuthenticationClient();
|
|
|
|
|
2019-12-30 11:38:55 -08:00
|
|
|
// @ts-ignore
|
2019-06-23 03:35:23 -07:00
|
|
|
const response = await Sheets.spreadsheets.values.batchUpdate(
|
|
|
|
{
|
|
|
|
auth: client,
|
|
|
|
spreadsheetId: this.id,
|
2019-10-20 12:21:55 -07:00
|
|
|
valueInputOption: valueInputMode,
|
2019-06-23 03:35:23 -07:00
|
|
|
resource: {
|
|
|
|
data: updateData,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
return response.data;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the cell values
|
|
|
|
*/
|
2019-10-20 12:21:55 -07:00
|
|
|
async setData(range: string, data: string[][], valueInputMode: ValueInputOption) {
|
2019-06-23 03:35:23 -07:00
|
|
|
const client = await this.getAuthenticationClient();
|
|
|
|
|
2019-12-30 11:38:55 -08:00
|
|
|
// @ts-ignore
|
2019-06-23 03:35:23 -07:00
|
|
|
const response = await Sheets.spreadsheets.values.update(
|
|
|
|
{
|
|
|
|
// @ts-ignore
|
|
|
|
auth: client,
|
|
|
|
spreadsheetId: this.id,
|
|
|
|
range,
|
2019-10-20 12:21:55 -07:00
|
|
|
valueInputOption: valueInputMode,
|
2019-06-23 03:35:23 -07:00
|
|
|
resource: {
|
|
|
|
values: data
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
return response.data;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Appends the cell values
|
|
|
|
*/
|
2019-10-20 12:21:55 -07:00
|
|
|
async appendData(range: string, data: string[][], valueInputMode: ValueInputOption) {
|
2019-06-23 03:35:23 -07:00
|
|
|
const client = await this.getAuthenticationClient();
|
|
|
|
|
2019-12-30 11:38:55 -08:00
|
|
|
// @ts-ignore
|
2019-06-23 03:35:23 -07:00
|
|
|
const response = await Sheets.spreadsheets.values.append(
|
|
|
|
{
|
|
|
|
auth: client,
|
|
|
|
spreadsheetId: this.id,
|
|
|
|
range,
|
2019-10-20 12:21:55 -07:00
|
|
|
valueInputOption: valueInputMode,
|
2019-06-23 03:35:23 -07:00
|
|
|
resource: {
|
|
|
|
values: data
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
return response.data;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the authentication client needed to access spreadsheet
|
|
|
|
*/
|
|
|
|
async getAuthenticationClient(): Promise<JWT> {
|
2019-10-27 13:44:21 -07:00
|
|
|
return getAuthenticationClient(this.credentials.email, this.credentials.privateKey, this.scopes);
|
2019-06-23 03:35:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the given sheet data in a strucutred way
|
|
|
|
*/
|
2019-08-27 11:27:00 -07:00
|
|
|
structureData(inputData: string[][], startRow: number, keys: string[], addEmpty?: boolean): IDataObject[] {
|
2019-06-23 03:35:23 -07:00
|
|
|
const returnData = [];
|
|
|
|
|
|
|
|
let tempEntry: IDataObject, rowIndex: number, columnIndex: number, key: string;
|
|
|
|
|
|
|
|
for (rowIndex = startRow; rowIndex < inputData.length; rowIndex++) {
|
|
|
|
tempEntry = {};
|
|
|
|
for (columnIndex = 0; columnIndex < inputData[rowIndex].length; columnIndex++) {
|
|
|
|
key = keys[columnIndex];
|
|
|
|
if (key) {
|
|
|
|
// Only add the data for which a key was given and ignore all others
|
|
|
|
tempEntry[key] = inputData[rowIndex][columnIndex];
|
|
|
|
}
|
|
|
|
}
|
2019-08-27 11:27:00 -07:00
|
|
|
if (Object.keys(tempEntry).length || addEmpty === true) {
|
2019-06-23 03:35:23 -07:00
|
|
|
// Only add the entry if data got found to not have empty ones
|
|
|
|
returnData.push(tempEntry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return returnData;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the given sheet data in a strucutred way using
|
|
|
|
* the startRow as the one with the name of the key
|
|
|
|
*/
|
|
|
|
structureArrayDataByColumn(inputData: string[][], keyRow: number, dataStartRow: number): IDataObject[] {
|
|
|
|
|
|
|
|
const keys: string[] = [];
|
|
|
|
|
|
|
|
if (keyRow < 0 || dataStartRow < keyRow || keyRow >= inputData.length) {
|
|
|
|
// The key row does not exist so it is not possible to strucutre data
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the keys array
|
|
|
|
for (let columnIndex = 0; columnIndex < inputData[keyRow].length; columnIndex++) {
|
|
|
|
keys.push(inputData[keyRow][columnIndex]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.structureData(inputData, dataStartRow, keys);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-10-20 12:21:55 -07:00
|
|
|
async appendSheetData(inputData: IDataObject[], range: string, keyRowIndex: number, valueInputMode: ValueInputOption): Promise<string[][]> {
|
2019-06-23 03:35:23 -07:00
|
|
|
const data = await this.convertStructuredDataToArray(inputData, range, keyRowIndex);
|
2019-10-20 12:21:55 -07:00
|
|
|
return this.appendData(range, data, valueInputMode);
|
2019-06-23 03:35:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates data in a sheet
|
|
|
|
*
|
|
|
|
* @param {IDataObject[]} inputData Data to update Sheet with
|
|
|
|
* @param {string} indexKey The name of the key which gets used to know which rows to update
|
|
|
|
* @param {string} range The range to look for data
|
|
|
|
* @param {number} keyRowIndex Index of the row which contains the keys
|
|
|
|
* @param {number} dataStartRowIndex Index of the first row which contains data
|
|
|
|
* @returns {Promise<string[][]>}
|
|
|
|
* @memberof GoogleSheet
|
|
|
|
*/
|
2019-10-20 12:21:55 -07:00
|
|
|
async updateSheetData(inputData: IDataObject[], indexKey: string, range: string, keyRowIndex: number, dataStartRowIndex: number, valueInputMode: ValueInputOption, valueRenderMode: ValueRenderOption): Promise<string[][]> {
|
2019-06-23 03:35:23 -07:00
|
|
|
// Get current data in Google Sheet
|
|
|
|
let rangeStart: string, rangeEnd: string;
|
|
|
|
let sheet: string | undefined = undefined;
|
|
|
|
if (range.includes('!')) {
|
|
|
|
[sheet, range] = range.split('!');
|
|
|
|
}
|
|
|
|
[rangeStart, rangeEnd] = range.split(':');
|
|
|
|
|
|
|
|
const keyRowRange = `${sheet ? sheet + '!' : ''}${rangeStart}${dataStartRowIndex}:${rangeEnd}${dataStartRowIndex}`;
|
|
|
|
|
2019-10-20 12:21:55 -07:00
|
|
|
const sheetDatakeyRow = await this.getData(keyRowRange, valueRenderMode);
|
2019-06-23 03:35:23 -07:00
|
|
|
|
|
|
|
if (sheetDatakeyRow === undefined) {
|
|
|
|
throw new Error('Could not retrieve the key row!');
|
|
|
|
}
|
|
|
|
|
|
|
|
const keyColumnOrder = sheetDatakeyRow[0];
|
|
|
|
|
|
|
|
const keyIndex = keyColumnOrder.indexOf(indexKey);
|
|
|
|
|
|
|
|
if (keyIndex === -1) {
|
|
|
|
throw new Error(`Could not find column for key "${indexKey}"!`);
|
|
|
|
}
|
|
|
|
|
|
|
|
const characterCode = rangeStart.toUpperCase().charCodeAt(0) + keyIndex;
|
|
|
|
let keyColumnRange = String.fromCharCode(characterCode);
|
|
|
|
keyColumnRange = `${sheet ? sheet + '!' : ''}${keyColumnRange}:${keyColumnRange}`;
|
|
|
|
|
2019-10-20 12:21:55 -07:00
|
|
|
const sheetDataKeyColumn = await this.getData(keyColumnRange, valueRenderMode);
|
2019-06-23 03:35:23 -07:00
|
|
|
|
|
|
|
if (sheetDataKeyColumn === undefined) {
|
|
|
|
throw new Error('Could not retrieve the key column!');
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: The data till here can be cached optionally. Maybe add an option which can
|
|
|
|
// can be activated if it is used in a loop and nothing else updates the data.
|
|
|
|
|
|
|
|
// Remove the first row which contains the key
|
|
|
|
sheetDataKeyColumn.shift();
|
|
|
|
|
|
|
|
// Create an Array which all the key-values of the Google Sheet
|
|
|
|
const keyColumnIndexLookup = sheetDataKeyColumn.map((rowContent) => rowContent[0] );
|
|
|
|
|
|
|
|
const updateData: ISheetUpdateData[] = [];
|
|
|
|
let itemKey: string | number | undefined | null;
|
|
|
|
let propertyName: string;
|
|
|
|
let itemKeyIndex: number;
|
|
|
|
let updateRowIndex: number;
|
|
|
|
let updateColumnName: string;
|
|
|
|
for (const inputItem of inputData) {
|
|
|
|
itemKey = inputItem[indexKey] as string;
|
|
|
|
// if ([undefined, null].includes(inputItem[indexKey] as string | undefined | null)) {
|
|
|
|
if (itemKey === undefined || itemKey === null) {
|
|
|
|
// Item does not have the indexKey so we can ignore it
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Item does have the key so check if it exists in Sheet
|
|
|
|
itemKeyIndex = keyColumnIndexLookup.indexOf(itemKey as string);
|
|
|
|
if (itemKeyIndex === -1) {
|
|
|
|
// Key does not exist in the Sheet so it can not be updated so skip it
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the row index in which the data should be updated
|
|
|
|
// TODO: Should probably change the indexes to be 1 based because Google Sheet is
|
|
|
|
updateRowIndex = keyColumnIndexLookup.indexOf(itemKey) + dataStartRowIndex + 1;
|
|
|
|
|
|
|
|
// Check all the properties in the sheet and check which ones exist on the
|
|
|
|
// item and should be updated
|
|
|
|
for (propertyName of keyColumnOrder) {
|
|
|
|
if (propertyName === indexKey) {
|
|
|
|
// Ignore the key itself as that does not get changed it gets
|
|
|
|
// only used to find the correct row to update
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (inputItem[propertyName] === undefined || inputItem[propertyName] === null) {
|
|
|
|
// Property does not exist so skip it
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Property exists so add it to the data to update
|
|
|
|
|
|
|
|
// Get the column name in which the property data can be found
|
2019-11-07 09:38:38 -08:00
|
|
|
updateColumnName = String.fromCharCode(rangeStart.toUpperCase().charCodeAt(0) + keyColumnOrder.indexOf(propertyName));
|
2019-06-23 03:35:23 -07:00
|
|
|
|
|
|
|
updateData.push({
|
|
|
|
range: `${sheet ? sheet + '!' : ''}${updateColumnName}${updateRowIndex}`,
|
|
|
|
values: [
|
|
|
|
[
|
|
|
|
inputItem[propertyName] as string,
|
|
|
|
],
|
|
|
|
],
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-20 12:21:55 -07:00
|
|
|
return this.batchUpdate(updateData, valueInputMode);
|
2019-06-23 03:35:23 -07:00
|
|
|
}
|
|
|
|
|
2019-08-27 11:27:00 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Looks for a specific value in a column and if it gets found it returns the whole row
|
|
|
|
*
|
|
|
|
* @param {string[][]} inputData Data to to check for lookup value in
|
|
|
|
* @param {number} keyRowIndex Index of the row which contains the keys
|
|
|
|
* @param {number} dataStartRowIndex Index of the first row which contains data
|
|
|
|
* @param {ILookupValues[]} lookupValues The lookup values which decide what data to return
|
2019-10-31 12:16:20 -07:00
|
|
|
* @param {boolean} [returnAllMatches] Returns all the found matches instead of only the first one
|
2019-08-27 11:27:00 -07:00
|
|
|
* @returns {Promise<IDataObject[]>}
|
|
|
|
* @memberof GoogleSheet
|
|
|
|
*/
|
2019-10-31 12:16:20 -07:00
|
|
|
async lookupValues(inputData: string[][], keyRowIndex: number, dataStartRowIndex: number, lookupValues: ILookupValues[], returnAllMatches?: boolean): Promise<IDataObject[]> {
|
2019-08-27 11:27:00 -07:00
|
|
|
const keys: string[] = [];
|
|
|
|
|
|
|
|
if (keyRowIndex < 0 || dataStartRowIndex < keyRowIndex || keyRowIndex >= inputData.length) {
|
|
|
|
// The key row does not exist so it is not possible to look up the data
|
|
|
|
throw new Error(`The key row does not exist!`);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the keys array
|
|
|
|
for (let columnIndex = 0; columnIndex < inputData[keyRowIndex].length; columnIndex++) {
|
|
|
|
keys.push(inputData[keyRowIndex][columnIndex]);
|
|
|
|
}
|
|
|
|
|
|
|
|
const returnData = [
|
|
|
|
inputData[keyRowIndex],
|
|
|
|
];
|
|
|
|
|
|
|
|
// Loop over all the lookup values and try to find a row to return
|
|
|
|
let rowIndex: number;
|
|
|
|
let returnColumnIndex: number;
|
|
|
|
lookupLoop:
|
|
|
|
for (const lookupValue of lookupValues) {
|
|
|
|
returnColumnIndex = keys.indexOf(lookupValue.lookupColumn);
|
|
|
|
|
|
|
|
if (returnColumnIndex === -1) {
|
|
|
|
throw new Error(`The column "${lookupValue.lookupColumn}" could not be found!`);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Loop over all the items and find the one with the matching value
|
|
|
|
for (rowIndex = dataStartRowIndex; rowIndex < inputData.length; rowIndex++) {
|
|
|
|
if (inputData[rowIndex][returnColumnIndex].toString() === lookupValue.lookupValue.toString()) {
|
|
|
|
returnData.push(inputData[rowIndex]);
|
2019-10-31 12:16:20 -07:00
|
|
|
|
|
|
|
if (returnAllMatches !== true) {
|
|
|
|
continue lookupLoop;
|
|
|
|
}
|
2019-08-27 11:27:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If value could not be found add an empty one that the order of
|
|
|
|
// the returned items stays the same
|
2019-10-31 12:16:20 -07:00
|
|
|
if (returnAllMatches !== true) {
|
|
|
|
returnData.push([]);
|
|
|
|
}
|
2019-08-27 11:27:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return this.structureData(returnData, 1, keys, true);
|
|
|
|
}
|
|
|
|
|
2019-06-23 03:35:23 -07:00
|
|
|
|
|
|
|
async convertStructuredDataToArray(inputData: IDataObject[], range: string, keyRowIndex: number): Promise<string[][]> {
|
|
|
|
let startColumn, endColumn;
|
|
|
|
let sheet: string | undefined = undefined;
|
|
|
|
if (range.includes('!')) {
|
|
|
|
[sheet, range] = range.split('!');
|
|
|
|
}
|
|
|
|
[startColumn, endColumn] = range.split(':');
|
|
|
|
|
|
|
|
|
|
|
|
let getRange = `${startColumn}${keyRowIndex + 1}:${endColumn}${keyRowIndex + 1}`;
|
|
|
|
|
|
|
|
if (sheet !== undefined) {
|
|
|
|
getRange = `${sheet}!${getRange}`;
|
|
|
|
}
|
|
|
|
|
2019-10-20 12:21:55 -07:00
|
|
|
const keyColumnData = await this.getData(getRange, 'UNFORMATTED_VALUE');
|
2019-06-23 03:35:23 -07:00
|
|
|
|
|
|
|
if (keyColumnData === undefined) {
|
|
|
|
throw new Error('Could not retrieve the column data!');
|
|
|
|
}
|
|
|
|
|
|
|
|
const keyColumnOrder = keyColumnData[0];
|
|
|
|
|
|
|
|
const setData: string[][] = [];
|
|
|
|
|
|
|
|
let rowData: string[] = [];
|
|
|
|
inputData.forEach((item) => {
|
|
|
|
rowData = [];
|
|
|
|
keyColumnOrder.forEach((key) => {
|
|
|
|
if (item.hasOwnProperty(key) && item[key]) {
|
|
|
|
rowData.push(item[key]!.toString());
|
|
|
|
} else {
|
|
|
|
rowData.push('');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
setData.push(rowData);
|
|
|
|
});
|
|
|
|
|
|
|
|
return setData;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|