Google脚本从电子表格中选择一些值并将其复制到另一张表格上的另一列 [英] Google Script Select and copy some values from spreadsheet comparing to another column on another sheet

查看:145
本文介绍了Google脚本从电子表格中选择一些值并将其复制到另一张表格上的另一列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已阅读这两个链接 Google脚本:有条件地将行从一个电子表格复制到另一个电子表格 https://stackoverflow.com/a/4809413/1526044 和尝试后,我无法做我所需要的。

我有一张电子表格,其中有一些数据在一张纸上。
另外我还有另一张关于列的关键信息来比较这种方式:

  KEY值1 Value2 Value3 
AAA BCD
BBB YZW

我需要一个函数来检查我的DATA电子表格行行查找键盘上列值的值。当找到任何值时,它会在DATA电子表格的第一列(同一行)上写下KEY值。

示例:
DATA电子表格


 空文本文本文本B 
空文本文本文本文本
空文本C文本文本
空文本文本文本Y

因此,在脚本之后,DATA将如下所示:

  AAA文本文本文本B 
空文本文本文本文本
AAA文本C文本文本
BBB文本文本Y

任何其他建议,将不胜感激。
非常感谢。

解决方案

我修改了一个类似的脚本,我最近回答说应该做你需要的。我没有测试,但你可以试试看。有一些意见可以解释这个想法......它可能需要一些调试。



编辑:我玩了这个并调试了它多一点;-)所以它似乎现在工作 - 这里是链接到我的测试()

$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ b和一个数组数据表,你得到这些东西像* /
var keysheet = SpreadsheetApp.getActiveSpreadsheet()。getSheets()[0] .getDataRange()。getValues();
//和
var datasheet = SpreadsheetApp.getActiveSpreadsheet()。getSheets()[1] .getDataRange()。getValues(); //如果其他列,更改数组中的索引值:0 = A,1 = B ...
//迭代可以是这样的:
for(i = 0; i< datasheet.length; ++ i){//不要错过任何数据(k = 1; k< keysheet [0] .length; +(< keyheet.length; ++ j); //遍历键表找到键
; + k){//在密钥表行中迭代以检查除第一列之外的每一列
var key = keysheet [j] [k]
Logger.log(k +''+ key ++ datasheet [i ] .toString()。match(key))
if(datasheet [i] .toString()。match(key)== key){//检查整行以查找key是否在数据
var x = datasheet [i] .shift();
datasheet [i] .unshift(keysheet [j] [0])//如果找到匹配,用键盘中的fisrt元素替换数据表中的第一个元素
break //如果发现break k loop也许你应该还打破j循环?
}

} //循环密钥表
} //循环数据表

/ *现在您有一个修改的数据表数组,可以直接覆盖旧数据表
使用setValues()* /
var sh = SpreadsheetApp.getActiveSpreadsheet()。getSheets()[1]; //假定datasheet表是表单nr2
Logger.log(datasheet )
sh.getRange(1,1,datasheet.length,datasheet [0] .length).setValues(datasheet);
}
//


I have read this two links Google Script: Conditionally copy rows from one spreadsheet to another and https://stackoverflow.com/a/4809413/1526044 and after trying i am not able to do what i need.

I have one spreadsheet with some data on one sheet. Also i have another sheet with key information on columns to compare this way:

KEY Value1 Value2 Value3 
AAA   B      C      D
BBB   Y      Z      W

I need a function that check the my DATA spreadsheet row by row looking for the values on the columns values on the key sheet. And when any of the values is found it writes down on the first column of the DATA spreadsheet (on the same row) the KEY value.

Example: DATA spreadsheet

empty text text text B
empty text text text text
empty text   C  text text
empty text text text Y

So after the script, DATA will look like this:

AAA text text text B
empty text text text text
AAA text   C  text text
BBB text text text Y

Any other suggestion to make this will be appreciated. Thank you very much.

解决方案

I modified a similar script I answered recently that should do what you need. I didn't test but you can give it a try. There are some comments to explain the idea... it probably needs some debugging.

EDIT : I played around with this and debugged it a bit more ;-) so it seems to work as needed now - here is the link to my test sheet

function xxx(){ 
/* we have an array 'keysheet'
    and an array datasheet , you get these with something like*/
        var keysheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0].getDataRange().getValues();
    // and
        var datasheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[1].getDataRange().getValues();// if other columns, change index values in the arrays : 0=A, 1=B ...
    // the iteration could be like this :
              for(i=0;i<datasheet.length;++i){ // don't miss any data
                for(j=0;j<keysheet.length;++j){ // iterate through keysheet to find the keys
                  for(k=1;k<keysheet[0].length;++k){ // iterate in keysheet row to check every column except first one
                      var key = keysheet[j][k]
                      Logger.log(k+' '+key+"  "+datasheet[i].toString().match(key))
                 if(datasheet[i].toString().match(key) == key){// check whole rows to find if key is in data
                  var x = datasheet[i].shift();
                   datasheet[i].unshift(keysheet[j][0]) // if match found, replace first element in datasheet with fisrt element in keysheet
                     break  // if found break k loop maybe you should also break j loop ?
                       }
                 }
                   } //loop keysheet
             } // loop datasheet

            /* now you have a modified datasheet array that can directly overwrite the old datasheet 
            using setValues() */
            var sh = SpreadsheetApp.getActiveSpreadsheet().getSheets()[1];// assuming the datasheet sheet is sheet nr2
            Logger.log(datasheet)
           sh.getRange(1,1,datasheet.length,datasheet[0].length).setValues(datasheet);
              }
        //

这篇关于Google脚本从电子表格中选择一些值并将其复制到另一张表格上的另一列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆