0
votes

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);
1
Instead of cleaning it up afterwards you could run a google script that writes the data needed by your python script into another sheet to make it easier to import. But deleting and sorting columns can be done with Range.moveTo and Range.sort. developers.google.com/apps-script/reference/spreadsheet/range - SpiderPig
Great idea, which I might need to go with. Got my function now - but would need to clear the sheet and then import another Google sheet daily. Any ideas on what would be needed to pop that in a function? //Clear sheet, ready for import clear(); // Import content from Google Sheet # ?? - me9867
You mean one of your sheets is dedicated to importing data and each day you want to import another sheets data into it? You can use Sheet.clear() and Range.copyTo() for that. - SpiderPig

1 Answers

1
votes
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);
}