简体   繁体   English

如何使用Google Apps脚本合并Google表格中的列值

[英]How do I Merge Column Values in Google Sheets with Google Apps Script

I recognize that this question is similar to this one . 我知道这个问题与相似。 I'm sad to say, I just don't know enough to follow that one. 我很伤心地说,我只是不知道够遵循一个。

I also recognize that it is very similar to the Google tutorial , but they are creating a new array, and I need to combine arrays. 我也认识到它与Google教程非常相似,但是他们正在创建一个新数组,我需要组合数组。

The problem 问题

I'm trying to create a permanent list from a volatile list. 我正在尝试从易失性列表中创建一个永久列表。 The permanent list will be updated daily. 永久列表将每天更新。 I need the script to look for duplicates on one sheet, and copy everything new to another sheet. 我需要脚本在一张纸上查找重复项,并将所有新内容复制到另一张纸上。 I'm pretty sure that my problem lies mostly in my logic. 我很确定我的问题主要出在我的逻辑上。

I've been bashing together all the similar answers I could find to help (commented out) and I just can't figure it out. 我一直在努力寻求所有类似的答案(提出来),我只是想不通。 More explanation below. 下面有更多解释。

Spreadsheet here 电子表格在这里

Script here 脚本在这里

Code.gs 代码

    function updateRoster() {
  var ss = SpreadsheetApp
    .openById("15DRZRQ2Hcd7MNnAsu_lnZ6n4kiHeXW_OMPP3squbTLE");
  var data = ss
    .getSheetByName("Volatile Data")
    .getDataRange()
    .getValues();
  var saveData = ss
    .getSheetByName("All Data")
    .getDataRange()
    .getValues();
  for (i in data) {
    var value = data[i][3];
    for (j in saveData) {
      var search = saveData[j][3]
      if(value == search) {
        Logger.log(data[i][3]);
      }
    }
  }
}

I get this in the Log: 我在日志中得到这个:

[16-03-24 06:35:44:914 PDT] 1.00000006E8 [PDT 16-03-24 06:35:44:914] 1.00000006E8

[16-03-24 06:35:44:916 PDT] 1.00000012E8 [PDT 16-03-24 06:35:44:916] 1.00000012E8

[16-03-24 06:35:44:918 PDT] 1.00000022E8 [PDT 16-03-24 06:35:44:918] 1.00000022E8

Which is correct, those are the duplicates, but I need to select every other row. 这是正确的,这些都是重复的,但我需要选择每隔一行。 The ones that are not duplicates. 那些不是重复的。

I've tried a bunch of changes, but I just keep getting the second sheet's list repeated. 我尝试了很多更改,但只是不断重复第二张工作表的列表。 Please help. 请帮忙。

Simply replacing value == search with value != search will not work since then it would add the row to the second sheet if even one of the rows in saveData is different. 简单地将value == search替换为value != search行不通的,因为即使saveData中的行之一不同,也会将行添加到第二张表中。 But it needs to check if all rows are different. 但是它需要检查所有行是否都不同。

If you are sure that your volatile data sheet doesn't have any duplicates in it, you can do it this way: 如果您确定易失性数据表中没有重复项,则可以通过以下方式进行操作:

var saveSheet = ss.getSheetByName("All Data");
loopThroughData:
for (i in data) {
  var value = data[i][3];
  for (j in saveData) {
    var search = saveData[j][3]
    if(value === search) continue loopThroughData;
    //here we leave the inner loop and jump back to the top if
    //even one row is equal
  }
  saveSheet.appendRow(data[i]);
}

edit: However it's usually better to use functions instead of a combination of continue and labels. 编辑:但是,通常最好使用函数,而不是continue和标签的组合。 Therefore I would prefer this solution: 因此,我更喜欢这种解决方案:

function updateRoster() {
  ...

  function areThereDuplicates(i) {
    var value = data[i][3];
    for(var j in saveData) {
      if(saveData[j][3] === value) return true;
    }
    return false;
  }

  for (i in data) {
    if(!areThereDuplicates(i)) saveSheet.appendRow(data[i]);
  }
}

Well to get every OTHER value you could check for every row that is Different or "new" 要获得每个其他值,您可以检查每行不同或“新”的值

if(value != search) {
    Logger.log(data[i][3]);
     }

and simply add the new rows to the file as 并简单地将新行添加为文件

 if(value != search) {
        Logger.log(data[i][3]);
        ss.getSheetByName("All Data").appendRow(data[i]);
      }

Good luck =) 祝你好运=)

暂无
暂无

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

相关问题 如何从表格中计算谷歌应用脚​​本中的持续时间? - How do I calculate duration in google apps script from sheets? 添加行时合并列的相同值的 Google 表格脚本 - Google Sheets script to merge identical values of a column when a row is added 如何在我的Google Apps脚本/ Google表格HTML中包含脚本? - How do I include scripts in my Google Apps Script/Google Sheets HTML? 如何使用单元格的应用程序脚本将 Google 表格数据验证添加到列中,以仅允许输入颜色和十六进制颜色代码 - How do I add Google sheets data vaildation to a column using apps script for cells to only allow colors and hex color codes to be inputted 如何通过 Apps Script 更改 Google Sheets 中的多个单元格值? - How to change multiple cell values in Google Sheets via Apps Script? 如何确定(New Apps)是否正在执行(Google Apps)脚本? - How do I determine whether a (Google Apps) script is being executed on New Sheets? 如何在电子表格文件中的所有工作表上运行 Google Apps 脚本? - How do I run a Google Apps script on all sheets in my spreadsheet file? Google Apps Script - 如何将此代码应用于 Google 表格中的多个表格 - Google Apps Script - how to applies this code for multiple sheets in google sheets 如何使用 Google Script 从 Google 表格中提取特定列? - How do I extract a specific Column from google sheets using Google Script? Google Apps 脚本 - 更改一系列 google 表格单元格中的值 - Google Apps Script - Changing values in a range of google sheets cells
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM