I've been making a cleaning algorithm in Google Apps Scripts to automate the trimming of a sheet that has been ripped from pdf.
I've spent a few hours now tinkering with this code I've got and just completely baffled as to where I've gone wrong.
Previous to this rough spot the case logic wasn't quite correct BUT the script was getting through the first if statement, second loop, second if statement, case statement; successfully deleting the non-useful rows. Back then it wasn't resetting i and j property so things got a little freaky.
Now that I've fixed up the case logic, its just looping through i and not recognizing to proceed to second loop via the first if statement and I'm STUMPED.
Apologies in advance if my formatting/documentation is gross; I'm pretty much self-taught and gladly welcome all criticism!
function ytdClean() {
var app = SpreadsheetApp;
var sheet = app.getActiveSpreadsheet().getSheetByName("Sheet1");
var sheetEnd = sheet.getLastRow();
for (var i = 1; i <= sheetEnd; i++) { //1. Loop i through column A 1:sheetEnd...
var x = sheet.getRange(i, 1);
Logger.log('x = ' + x.getValue());
if (x.getValue() == '') { //...until null entry x
for (var j = x.getRow + 1; j <= sheetEnd; j++) { //2. Loop j through column A from x + (1,0)...
var y = sheet.getRange(j, 1);
var z = sheet.getRange(j + 1, 1);
Logger.log('y = ' + y.getValue());
if (y.getValue() != '') { //...until non-null entry y;
var switchVal = z.getValue();
Logger.log('z = ' + switchVal + '...');
switch (switchVal) { //3. Switch
case "001": //case: z = y + (1,0) = 001...
var a = x.getRow();
var b = y.getRow() - 1;
sheet.deleteRows(a, b - a); //...delete rows of x through y...
i = a - 1;
Logger.log("...case 001");
break; //...break;
case "01": //case: z = 01...
var a = x.getRow();
var b = sheetend;
sheet.deleteRows(a, b - a); //...delete rows between and including x, y and all rows after y;
i = a - 1;
Logger.log("...case 01");
break; //...break;
default: //case: 001 < z < 1000...
var a = x.getRow();
var b = y.getRow();
sheet.deleteRows(a, b - a); //...delete rows of x through y...
i = a - 1;
Logger.log("...default");
break; //...break;
}
}
}
}
}
}
Can anybody see what I'm doing wrong here? I've tried using [null] instead of [''] within the if statements and that doesn't change a thing. Still doesn't give a damn about those if statements...