I am fairly new to script. I have the following script which duplicates the active spreadsheet to another tab and then deletes the columns out of a varying range. Essentially I want it to delete from column 12 up to the 3rd to the last column in the spreadsheet. This portion is working correctly, but it takes almost 20 seconds to run. Is there any way I could optimize this for faster processing speeds?
The second part of the script searches the rows of the spreadsheet, and if Column A is empty or has an "x" in it, deletes that row. This portion works correct but I would like it to hide the row rather than delete it and can't seem to figure it out.
function NewWeek() {
//Duplicate the sheet
SpreadsheetApp.getActiveSpreadsheet().duplicateActiveSheet();
//Delete Future Weeks
var sheet = SpreadsheetApp.getActiveSheet();
var columns = sheet.getDataRange();
var numColumns = columns.getNumColumns();
var columnsDeleted = 0;
for (var i = 14; i <= numColumns - 3; i++) {
sheet.deleteColumn((parseInt(i) + 1) - columnsDeleted);
columnsDeleted++;
}
var sheet = SpreadsheetApp.getActiveSheet();
var rows = sheet.getDataRange();
var numRows = rows.getNumRows();
var values = rows.getValues();
var rowsDeleted = 0;
for (var i = 4; i <= numRows - 1; i++) {
var row = values[i];
if (row[0] == 0 || row[0] == 'x') {
sheet.deleteRow((parseInt(i) + 1) - rowsDeleted);
rowsDeleted++;
}
}
}