简体   繁体   中英

Google apps script - Mapping arrays

I am working on a project where I take multiple column/row inventory sheets and turn them into a multi-row/2-column format for order picking. I have a switch for selecting the appropriate inventory sheet and a map() function that copies the imported information from the inventory DataRange().

However, not all the data is in consistent columns. What I would like to do is find an expression that maps the next column in if the column it was mapping has a zero or "" value.

I won't give you the full body of code unless you need it, but hopefully just the important parts. This is what I have:

    var source = SpreadsheetApp.openById("1xixIOWw2yGd1aX_2HeguZnt8G_UfiFOfG-W6Fk8OSTs"); //This sheet
    var srcSht = SpreadsheetApp.getActive();
    var sourceMenu = srcSht.getRange('A1');//This is the cell cotaining the dropdown
    var menuTest = sourceMenu.getValue();
  // Variable for the vars sheet. If it doesn't exist, create it
    var varsTest = source.getSheetByName('vars');
        if (!varsTest){source.insertSheet('vars');} 
    var importedA1 = varsTest.getDataRange().getA1Notation();
    varsTest.clearContents();

    var t1Imp = '=ImportRange("test1_Id", "Stock!A1:F11")';
    var varsData = varsTest.getRange('A1');// This is the cell we fill with the importRange formula


    varsData.setValue(t1Imp);
    var imported = varsTest.getDataRange().getValues();
    var newOrder = imported.map(function(item) {
        if (item[4] !== NaN){return [[item[0]],[item[4]]];};
        if (item[4] === NaN){return [[item[0]],[item[3]]];};}                         

    var orderRange = source.getSheetByName('Sheet1').getRange(10,1,newOrder.length, newOrder[0].length);

    orderRange.setValues(newOrder);
        Logger.log("\t" + newOrder);


Logger.log(newOrder): [(timestamp omitted)] items1,order,caramel,6,c&c,2,mint,3,PB,0,,,items2,,caramel,,strawberry,,mint,,PB,

It seems to be skipping the if statements, or I told it that I mean to test the index as NaN, which will obviously never be true.

I also tried replacing 'NaN' with 'undefined'. Same result. I tried finding the item[4].Values, but it gave me an error. I also tried the same logic using filter() instead of map() but it copied the entire data set.

I pull these values onto a new 'vars' sheet in the workbook (to minimize calls to the web service): test1 reduce them to the first and last columns, then output: test

The cells in the 'order' column for the second set of items in the 'test' sheet are blank. The values for that order column should be in item[3] of that array, but I can't get the script to identify that that the blank cells are blank.

I am new to Google Apps Script and JS, but I am watching a lot of tuts and learning by doing. If I find a solution, I will post it.

Thank you StackOverflow, I could not have learned as much as I have without this community!

I have a working function that does what I want. In short: I had to create a duplicate of the order column in a new column, so that all the values would line up. It's not technically a JS answer, but was the simplest and follows good spreadsheet rules.

function rmZeroOrderPS(item){
    var source = SpreadsheetApp.openById("<sheetId>"); //This sheet
    var varsTest = source.getSheetByName('vars');
    var imported = varsTest.getDataRange().getValues();
    var i=-1;
    while (i <= imported.length){ 
      if(item[8]!= 0) {return [item[0],item[8]]};      
    i+=1;    
    };

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