mirror of
https://github.com/n8n-io/n8n.git
synced 2024-11-09 22:24:05 -08:00
Added 'delete all empty rows' function to Google Sheets node
This commit is contained in:
parent
cef64329a9
commit
64527425b7
|
@ -52,9 +52,6 @@ export async function router(this: IExecuteFunctions): Promise<INodeExecutionDat
|
|||
case 'create':
|
||||
sheetName = spreadsheetId;
|
||||
break;
|
||||
case 'delete':
|
||||
sheetName = sheetId;
|
||||
break;
|
||||
case 'remove':
|
||||
sheetName = `${spreadsheetId}||${sheetId}`;
|
||||
break;
|
||||
|
|
|
@ -33,6 +33,18 @@ export const description: SheetProperties = [
|
|||
default: 'rows',
|
||||
description: 'What to delete',
|
||||
},
|
||||
{
|
||||
displayName: 'Delete Empty Rows',
|
||||
name: 'deleteEmptyRows',
|
||||
type: 'boolean',
|
||||
default: false,
|
||||
description: 'Delete all empty rows',
|
||||
displayOptions: {
|
||||
show: {
|
||||
toDelete: ['rows']
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
displayName: 'Start Row Number',
|
||||
name: 'startIndex',
|
||||
|
@ -49,6 +61,7 @@ export const description: SheetProperties = [
|
|||
toDelete: ['rows'],
|
||||
},
|
||||
hide: {
|
||||
deleteEmptyRows: [true],
|
||||
...untilSheetSelected,
|
||||
},
|
||||
},
|
||||
|
@ -68,6 +81,7 @@ export const description: SheetProperties = [
|
|||
toDelete: ['rows'],
|
||||
},
|
||||
hide: {
|
||||
deleteEmptyRows: [true],
|
||||
...untilSheetSelected,
|
||||
},
|
||||
},
|
||||
|
@ -114,6 +128,7 @@ export async function execute(
|
|||
this: IExecuteFunctions,
|
||||
sheet: GoogleSheet,
|
||||
sheetName: string,
|
||||
sheetId: string //router always gives us both name and id
|
||||
): Promise<INodeExecutionData[]> {
|
||||
const items = this.getInputData();
|
||||
|
||||
|
@ -123,25 +138,55 @@ export async function execute(
|
|||
const deleteType = this.getNodeParameter('toDelete', i) as string;
|
||||
|
||||
if (deleteType === 'rows') {
|
||||
startIndex = this.getNodeParameter('startIndex', i) as number;
|
||||
// We start from 1 now...
|
||||
startIndex--;
|
||||
numberToDelete = this.getNodeParameter('numberToDelete', i) as number;
|
||||
if (numberToDelete === 1) {
|
||||
endIndex = startIndex + 1;
|
||||
} else {
|
||||
endIndex = startIndex + numberToDelete;
|
||||
const deleteEmptyRows = this.getNodeParameter('deleteEmptyRows', i) as boolean;
|
||||
|
||||
if (deleteEmptyRows){
|
||||
const range = sheetName; //full sheet
|
||||
const response = (await sheet.getData(range, 'UNFORMATTED_VALUE', 'FORMATTED_STRING'));
|
||||
const rows = response || [];
|
||||
const emptyRows: number[] = [];
|
||||
|
||||
// Identify empty rows
|
||||
rows.forEach((row, index) => {
|
||||
if (!row.length)
|
||||
emptyRows.push(index + 1); // Store the row number (1-indexed)
|
||||
});
|
||||
|
||||
// Delete empty rows (from bottom to top to avoid indices moving)
|
||||
for(let rowIndex of emptyRows.reverse()){
|
||||
requests.push({
|
||||
deleteDimension: {
|
||||
range: {
|
||||
sheetId: sheetId,
|
||||
dimension: 'ROWS',
|
||||
startIndex: rowIndex-1,
|
||||
endIndex: rowIndex,
|
||||
}
|
||||
}
|
||||
})
|
||||
};
|
||||
}
|
||||
requests.push({
|
||||
deleteDimension: {
|
||||
range: {
|
||||
sheetId: sheetName,
|
||||
dimension: 'ROWS',
|
||||
startIndex,
|
||||
endIndex,
|
||||
else{
|
||||
startIndex = this.getNodeParameter('startIndex', i) as number;
|
||||
// We start from 1 now...
|
||||
startIndex--;
|
||||
numberToDelete = this.getNodeParameter('numberToDelete', i) as number;
|
||||
if (numberToDelete === 1) {
|
||||
endIndex = startIndex + 1;
|
||||
} else {
|
||||
endIndex = startIndex + numberToDelete;
|
||||
}
|
||||
requests.push({
|
||||
deleteDimension: {
|
||||
range: {
|
||||
sheetId: sheetId,
|
||||
dimension: 'ROWS',
|
||||
startIndex,
|
||||
endIndex,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
});
|
||||
}
|
||||
} else if (deleteType === 'columns') {
|
||||
startIndex = this.getNodeParameter('startIndex', i) as string;
|
||||
numberToDelete = this.getNodeParameter('numberToDelete', i) as number;
|
||||
|
@ -154,7 +199,7 @@ export async function execute(
|
|||
requests.push({
|
||||
deleteDimension: {
|
||||
range: {
|
||||
sheetId: sheetName,
|
||||
sheetId: sheetId,
|
||||
dimension: 'COLUMNS',
|
||||
startIndex,
|
||||
endIndex,
|
||||
|
|
Loading…
Reference in a new issue