简体   繁体   中英

GAS Spreadsheet avoid getting duplicates by marking as “SENT”, not working?

I have this script in Google Spreadsheet and it fetches all the rows marked "READY" just fine, then it sets the value in column "W"(23) to "SENT", and then I am trying to avoid fetching duplicates, by marking the column as "SENT" but then when I run the code again, it ignores the "SENT" that it just pasted? What is wrong here?

var ss = SpreadsheetApp.openById("12y85GmJ94s6k3213j2nGK8rFr0GOfd_Emfk8WHu_MUQ");
var stitchSheet = ss.getSheetByName("Sheet8");
var orderSheet = ss.getSheetByName("Sheet1");

var SENT = "SENT";

function getOrders() {

    var range  = orderSheet.getDataRange();
    var orders  = range.getValues();  

    for (var i = 1; i < orders.length; i++) {
        var row = orders[i];
        var status = row[1];
        var order = row[4]; 
        var name = row[5]; 
        var system = row[22]; 

        if(system != SENT){ 
            if(status.toString() === 'READY'){
                orderSheet.getRange(i,23).setValue(SENT);
                stitchSheet.appendRow([order,name]); 
            }
        }
    }
}

Your code is fine, so there must be a logical error somewhere. I've noticed that you made var i in the for loop 1 . I do not know if this is intentional or not, but the index of arrays pretty much always starts with 0 in most programming languages, which means that you'll start at row 2 of your sheet, not row 1.

Finding logical errors

To find logical errors you need to learn how to use the debugger console in the script editor.

Put breakpoints on the lines I mark with a star below:

var ss = SpreadsheetApp.openById("12y85GmJ94s6k3213j2nGK8rFr0GOfd_Emfk8WHu_MUQ");
var stitchSheet = ss.getSheetByName("Sheet8");
var orderSheet = ss.getSheetByName("Sheet1");

var SENT = "SENT";

function getOrders() {

    var range  = orderSheet.getDataRange();
*   var orders  = range.getValues();  

    for (var i = 1; i < orders.length; i++) {
*       var row = orders[i];
        var status = row[1];
        var order = row[4]; 
        var name = row[5]; 
*       var system = row[22]; 

        if(system != SENT){ 
            if(status.toString() === 'READY'){
                orderSheet.getRange(i,23).setValue(SENT);
                stitchSheet.appendRow([order,name]); 
            }
        }
    }
}

Start the debugger and it will stop at the first breakpoint. Inspect what value range has. Expanding This should let you find orderSheet and SENT (since they are outside the function and should be in the scope). If not you've got a problem here.

Go to the next breakpoint and inspect orders , it should have an array of arrays now. You can inspect that you've got the right values, if not, skip forward to the next breakpoint and look at what row is.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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