简体   繁体   English

使用JavaScript在Google表格中清理数据

[英]Data cleaning in Google Sheets using JavaScript

I've been making a cleaning algorithm in Google Apps Scripts to automate the trimming of a sheet that has been ripped from pdf. 我一直在Google Apps脚本中制定一种清理算法,以自动修剪从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; 在此问题之前,案例逻辑不是很正确,但是脚本正在通过第一个if语句,第二个循环,第二个if语句,case语句; successfully deleting the non-useful rows. 成功删除无用的行。 Back then it wasn't resetting i and j property so things got a little freaky. 那时并没有重置i和j属性,所以情况有点怪异。

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. 现在,我已经修复了案例逻辑,它只是循环遍历i,并且无法识别通过第一条if语句进行第二遍循环,并且我被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. 我已经尝试过在if语句中使用[null]而不是[''],但这不会改变任何事情。 Still doesn't give a damn about those if statements... 仍然不对那些if语句感到遗憾...

Google Sheets handle data in a different way than JavaScript. Google表格处理数据的方式与JavaScript不同。 There is no null in Google Sheets, but Google表格中没有null ,但是

  • Range.getValue() will return '' (an empty string) Range.getValue()将返回'' (一个空字符串)

    • 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 Range.isBlank()将返回

    • true for a blank cell 对于空白单元格为true
    • false for a cell containing ="" 对于包含=""的单元格为false
    • false for a cell containing ' (an apostrophe) 对于包含' (撇号)的单元格为false

By the other hand, JavaScript has two "equality" operators, 另一方面,JavaScript具有两个“相等”运算符,

  • == (equality aka abstract equality) == (相等又称为抽象相等)
  • === (identity ak strict equality) === (身份ak严格相等)

Related 有关

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM