根据另一个选项卡上的内容更新字段 [英] Updating a field based on contents on another tab

查看:84
本文介绍了根据另一个选项卡上的内容更新字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的'Source'选项卡1包含'Id'和'Name'列
My'Target 'Tab 2 contains'Id'



我尝试遍历目标标签中的行并读取Id的值,然后在Source标签页中搜索ID。当我找到Id时,我想获取'Name'字段中的值,并将其添加到我的Target Tab的Id中同一行的单元格中。



I我很难通过遍历一个数组的迭代逻辑工作,并确保我在目标选项卡或目标选项卡内容的数组中找到值。在目标选项卡中,可能有多个Id出现,我需要全部更新它们。



欢迎任何建议!

解决方案

以下是一个建议:

  / *让我们说源数组的名称(columnA)& ID(columnB)是数组'source'
,并且只有ID的目标数组是'target'数组,您可以使用类似* /
的方式获得这些数据var source = SpreadsheetApp.getActiveSpreadsheet()。getSheets()[ 0] .getDataRange()的GetValues();
//和
var target = SpreadsheetApp.getActiveSpreadsheet()。getSheets()[1] .getDataRange()。getValues(); //如果其他列改变了数组中的索引值:0 = A,1 = B ...
//然后让我们创建一个第3个数组,它将成为带有ID +名称的新目标,并称之为'newtarget'
var newtarget = new Array()
//迭代可能是这样的:
for(i = 0; i< target.length; ++ i){//不要错过任何ID
for(j = 0 ; j
if(target [i] [0] .toString()。match(source [j] [ 1] .toString())== source [j] [1] .toString()){
var newtargetrow = [source [j] [0],target [i] [0]] // if match找到并存储名称(idx0)和ID(idx 1)
} else {
var newtargetrow = ['找不到名称',target [i] [0]] //如果不匹配,在名称栏中显示
}
newtarget.push(newtargetrow); // stor e导致新的数组有2列
} //循环源
} //循环目标

/ *现在你有一个newtarget数组可以直接覆盖旧的目标
using setValues()* /
var sh = SpreadsheetApp.getActiveSpreadsheet()。getSheets()[1]; //假设目标图纸是表单nr2
sh.getRange(1,1, newtarget.length,NEWTARGET [0]。长度).setValues(NEWTARGET);
//

注意我没有测试这段代码,但它应该给你一个启动点。我使用.match比较了字符串,但可以使用其他比较(数组元素之间的直接相等)......如果在工作表数据中存在意外空间的风险,也可以删除ID中的空格...我不'不需要知道您的数据,这取决于您。


I've built a spreadsheet with multiple tabs.

My 'Source' Tab 1 contains columns 'Id' and 'Name' My 'Target' Tab 2 contains 'Id'

I'm attempting to iterate through the rows in my Target Tab and read the value of the Id and then search my Source Tab for the Id. When I find the Id, I want to grab the value in the 'Name' field and add it to a cell on the same line of my Target Tab's Id.

I'm having a hard time working through the logic of iterating through one array and ensuring I find the value in either the Target tab or an array of the Target Tabs contents. In the Target tab, there may be more than one occurrence of an Id, and I would need to update them all.

Any suggestions would be welcome!

解决方案

here is a suggestion :

/* let us say source array with name(columnA) & ID(columnB) is array 'source'
and target array with only IDs is array 'target', you get these with something like*/
    var source = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0].getDataRange().getValues();
// and
    var target = SpreadsheetApp.getActiveSpreadsheet().getSheets()[1].getDataRange().getValues();// if other columns, change index values in the arrays : 0=A, 1=B ...
//   then let's create a 3 rd array that will be the new target with ID + names, and call it 'newtarget' 
    var newtarget=new Array()
// the iteration could be like this :
          for(i=0;i<target.length;++i){ // don't miss any ID
           for(j=0;j<source.length;++j){ // iterate through source to find the name

             if(target[i][0].toString().match(source[j][1].toString()) == source[j][1].toString()){
               var newtargetrow=[source[j][0],target[i][0]] // if match found, store it with name (idx0) and ID (idx 1)
                   }else{
               var newtargetrow=['no name found',target[i][0]] // if no match, show it in name column
                   }
             newtarget.push(newtargetrow);// store result in new array with 2 columns
               } //loop source
         } // loop target

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

note I didn't test this code but it should give you a starting point. I made comparison on strings with a .match but you could use other comparisons (direct equality between array elements)... you could also remove spaces in IDs if there is a risk of undesired spaces in the sheet data... I don't know your data so it's up to you.

这篇关于根据另一个选项卡上的内容更新字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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