1

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...

Rubén
  • 34,714
  • 9
  • 70
  • 166
han_yolo_
  • 13
  • 3
  • Could you perform a log at each step that runs `typeof x.getValue()` for `x`, `y` and `z`? Or maybe just `typeof switchVal`. Should help predict what the comparison will output. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof – Sam Holmes Jun 27 '18 at 00:45
  • 1
    Well for starters `x.getValue() == ""` is true if the value is `0`, `undefined`, `null`, `""`/`''`, or `false`... Please review comparison operators in JavaScript (specifically "truthy" and "falsy"). Also note that if you delete rows while iterating forward, you should also update your end target. – tehhowch Jun 27 '18 at 00:57

1 Answers1

0

Google Sheets handle data in a different way than JavaScript. There is no null in Google Sheets, but

  • Range.getValue() will return '' (an empty string)

    • for a blank cell
    • for a cell containing =""
    • for a cell containing ' (an apostrophe).

In some circumstances the above is fine but in others not.

Also it's worth to note that

  • Range.isBlank() will return

    • true for a blank cell
    • false for a cell containing =""
    • false for a cell containing ' (an apostrophe)

By the other hand, JavaScript has two "equality" operators,

  • == (equality a.k.a abstract equality)
  • === (identity a.k. strict equality)

Related

Rubén
  • 34,714
  • 9
  • 70
  • 166