Google Spreadsheet脚本将A列中包含"Hello"的单元格与B列中的相邻单元格合并 [英] Google Spreadsheet script to merge cells in column A containing 'Hello' with the adjacent cell in column B

查看:71
本文介绍了Google Spreadsheet脚本将A列中包含"Hello"的单元格与B列中的相邻单元格合并的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将A列中包含某个单词的单元格(例如"Hello")与最右边的单元格(在B列中)合并

I'm trying to merge cells containing a certain word in column A (e.g. 'Hello') with the cell to the immediate right (in column B)

例如A4 = 'Hello',因此我想合并单元格A4和B4.

E.g. A4 = 'Hello', therefore I would like to merge cells A4 and B4.

到目前为止,我已经有了以下代码:

I have this code so far:

function formatCells() {

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var s = ss.getSheetByName('Combined');
  var range = s.getDataRange()
  var values = range.getValues();

  for( var row = values.length -1; row >= 0; --row )
    if (values[row][1] == 'Hello')
      {s.getRange(row+1,1).mergeAcross();
}
}

但是代码似乎根本没有做任何事情?外面有人可以告诉我我在做什么错吗?

But the code doesn't seem to be doing anything at all? Can anyone out there kindly tell me what I'm doing wrong?

非常感谢您的光临.

推荐答案

数组的索引为0,所以列A的索引为0 ... 您只需在您的情况下使用values[row][0].

Arrays are 0 indexed,so column A is index 0... You should simply use values[row][0] in your condition.

要合并两个像元,您需要获得2个像元的范围:

And to mergeAcross two cells you'll need to get a 2 cells range like this :

s.getRange(row+1,1,1,2).mergeAcross();

还请注意,由于merge方法不会合并内容,因此您将失去B列中的值.我不知道塔尔对您来说没有问题...

Note also that you will loose the value that was in column B since the merge method doesn't merge contents. I don't know if thar's an issue for you...

这篇关于Google Spreadsheet脚本将A列中包含"Hello"的单元格与B列中的相邻单元格合并的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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