简体   繁体   English

Google 应用程序脚本 - 映射数组

[英]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.我正在做一个项目,在该项目中,我采用多列/行库存表并将它们转换为多行/2 列格式以进行订单拣选。 I have a switch for selecting the appropriate inventory sheet and a map() function that copies the imported information from the inventory DataRange().我有一个用于选择适当库存表的开关和一个 map() 函数,用于从库存 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, Logger.log(newOrder): [(时间戳省略)] 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.它似乎跳过了 if 语句,或者我告诉它我的意思是将索引测试为 NaN,这显然永远不会是真的。

I also tried replacing 'NaN' with 'undefined'.我还尝试用“未定义”替换“NaN”。 Same result.结果一样。 I tried finding the item[4].Values, but it gave me an error.我试图找到 item[4].Values,但它给了我一个错误。 I also tried the same logic using filter() instead of map() but it copied the entire data set.我还使用 filter() 而不是 map() 尝试了相同的逻辑,但它复制了整个数据集。

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我将这些值拉到工作簿中的新“vars”表上(以尽量减少对 Web 服务的调用): test1将它们减少到第一列和最后一列,然后输出: 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.该订单列的值应该在该数组的 item[3] 中,但我无法让脚本识别空白单元格为空白。

I am new to Google Apps Script and JS, but I am watching a lot of tuts and learning by doing.我是 Google Apps Script 和 JS 的新手,但我正在观看很多教程并边做边学。 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!谢谢 StackOverflow,如果没有这个社区,我不可能学到这么多!

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.从技术上讲,这不是 JS 答案,但它是最简单的,并遵循良好的电子表格规则。

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;    
    };

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

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