简体   繁体   English

如何将拆分的Google Spreadsheet单元格值推到相邻单元格

[英]How to Push Split Google Spreadsheet Cell Value to Adjacent Cell

I am using a modified version of Martin Hawksey's Google Apps Script that takes the data in one of my sheets in a Google Spreadsheet another sheet makes a new row for each entry in my event description column (as separated by a comma - ie cell D2="Description 1, Description 2, Description 3, etc.). While this works very well, I would like for it to do one more function - I would like for it to take the last entry in the event description (perhaps separated from the others by a semicolon instead of a comma) and push it into the adjacent cell once and only once for each unique event while deleting the semicolon or other separator. I've tried a number of approaches, but being new to Google Apps Script, I am not having success. 我使用的是马丁·霍克西(Martin Hawksey)的Google Apps脚本的修改版,该版本将Google电子表格中一个工作表中的数据获取,另一个工作表在事件描述列中为每个条目添加了新行(用逗号分隔,即单元格D2 = “描述1,描述2,描述3等。)虽然效果很好,但我希望它可以执行其他功能-我希望它获取事件描述中的最后一个条目(可能与事件描述分开)并使用分号(而不是逗号)将它压入相邻的单元格,并且每次删除唯一的事件时都只将其一次,同时删除分号或其他分隔符。我尝试了多种方法,但是对于Google Apps脚本来说,没有成功。

function splitColumnAndRepeatRows(anArray, splitColumnIndex) {
  var output = [];
  for (i in anArray){ // for each row
    var splitArray = anArray[i][splitColumnIndex].toString().split(","); // split values in specified column
    for (j in splitArray){ // for each split cell value
      if(splitArray[j]=="" && j>=1)
        continue;
      var row = anArray[i].slice(0); // take a copy of source row
      row[splitColumnIndex] = alltrim(splitArray[j]); // replace comma separated value with current split value
      output.push(row); // push new row to output
    }
  }
  return output;
}

function alltrim(str) {
  return str.replace(/^\s+|\s+$/g, '');
}

Essentially, this is what I'm trying to do - turn this: 本质上,这就是我要尝试的操作-将此操作转为:

Date     Client     County     Description
9/21/14  12345      Greene     Location 1, Location 2, Location 3; Y
9/24/14  54321      Brown      Location 1, Location 2; X

into this: 到这个:

Date     Client     County     Description     Time
9/21/14  12345      Greene     Location 1
9/21/14  12345      Greene     Location 2
9/21/14  12345      Greene     Location 3      Y
9/24/14  54321      Brown      Location 1
9/24/14  54321      Brown      Location 2      X

Any help would be greatly appreciated! 任何帮助将不胜感激!

Thank you! 谢谢!

This edited splitColumnAndRepeatRows() function will do what you are after, with an added bonus that any (not just the last one) comma-separated item in your column can be a ;-delimited list and will be separated into 2 cells. 这个经过编辑的splitColumnAndRepeatRows()函数将完成您的工作,并增加了一个好处,即您列中的任何(不仅是最后一个)逗号分隔的项目都可以是一个用逗号分隔的列表,并将其分成2个单元格。

function splitColumnAndRepeatRows(anArray, splitColumnIndex) {
  var output = [];
  for (var i in anArray){ // for each row
    var splitArray = anArray[i][splitColumnIndex].toString().split(","); // split values in specified column
    for (var j in splitArray){ // for each split cell value
      if(splitArray[j]=="" && j>=1)
        continue;
      var row = anArray[i].slice(0); // take a copy of source row
      var trimmedString = alltrim(splitArray[j]);
      var subArray = trimmedString.split(";"); // split current item of specified column at ; 
      if ( subArray.length > 1 ) { // if current item is a ;-delimited list (now split into an array)
        row[splitColumnIndex] = alltrim(subArray[0]); // replace comma-separated value with first item of ;-delimited array
        row[splitColumnIndex+1] = alltrim(subArray[1]); // append second item of ;-delimited list as new cell to row
      }
      else {
        row[splitColumnIndex] = trimmedString; // replace comma separated value with current split value
        row[splitColumnIndex+1] = ""; // append empty cell to row
      }
      output.push(row); // push new row to output
    }
  }
  return output;
}

It can turn something like this: 它可以变成这样:

9/21/14  12345      Greene     Location 1; A, Location 2, Location 3; B
9/24/14  54321      Brown      Location 1, Location 2; C

Into this: 变成这个:

9/21/14  12345      Greene     Location 1      A
9/21/14  12345      Greene     Location 2
9/21/14  12345      Greene     Location 3      B
9/24/14  54321      Brown      Location 1
9/24/14  54321      Brown      Location 2      C

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

相关问题 如何使用Google可视化API检索Google电子表格单元格值? - How to use Google visualization API to retrieve google spreadsheet cell value? 如何在提交表单时从Google电子表格的单元格中获取有效值? - How to get the active value from a cell in a Google spreadsheet on form submission? 使用onEdit触发器将值返回到Google电子表格中的单元格 - Return a value to a cell in google spreadsheet with an onEdit trigger 在电子表格Google Appscript中增加单元格值 - Increment cell value in spreadsheet Google Appscript 如果值是日期,则清除相邻单元格的单元格值 - Clear cell value of adjacent cell if value is a date 如何根据所选单元格的值在电子表格单元格上创建注释 - How to create a note on a Spreadsheet cell based on the value of the selected cell 如何在 javascript 中使用电子表格单元格值定义变量 - How to define a variable with spreadsheet cell value in javascript 如何从谷歌表格中另一个单元格的相邻单元格中分配变量的值 - How to assign a the value of a variable from the adjacent cells of another cell in google sheets 使用 v 4 API 从公共 Google 电子表格中获取单元格值 - Get a cell value from a public Google spreadsheet using v 4 API 我可以从网络服务值中填充谷歌电子表格单元格吗? - Can I fill a google spreadsheet cell from a webservice Value?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM