me9867
me9867

Reputation: 1599

Delete multiple columns, then sort by price in Google Sheets (Script Editor)

I currently run a simple daily script to sort my products by price. This is done with freezing the first row (headers).

function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

// Sorts the sheet by the column I - the ninth across - , in ascending order 
sheet.sort(9);
}

I need to import some data now with a Python script which has issues dealing with frozen rows. So I need to run a cleanup script when it has finished, within my Google Sheet.

How can I delete columns D, F, H and then sort by price in column I (without having to freeze the top row) all with a single Google Script?

Part 1https://developers.google.com/apps-script/reference/spreadsheet/sheet

sheet.deleteColumn(4,6,8);

Part 2 - possible header ignore Google Apps Script: Can I specify a header row when sorting in a script?

sheet.getRange(2, 1, height-1, width+1).sort(width+1);

Upvotes: 0

Views: 1403

Answers (1)

me9867
me9867

Reputation: 1599

function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];

//Clear sheet, ready for import
clear();

// Import content from Google Sheet
??

// Delete unused columns
sheet.deleteColumn(4);
sheet.deleteColumn(5);
sheet.deleteColumn(6);

// Freeze first row
sheet.setFrozenRows(1);

// Sort by price
sheet.sort(4);

// Unfreeze first row
sheet.setFrozenRows(0);
}

Upvotes: 1

Related Questions