如何将列复制到另一列的第一个空白单元格? [英] How to copy column to the first blank cell of another column?

查看:168
本文介绍了如何将列复制到另一列的第一个空白单元格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在A列中有一些要复制B列的第一个空单元格的值.

我设法解决了问题的第一部分:

I managed to get the first part of the problem:

function CopyValues() {
  var ss = SpreadsheetApp.getActiveSpreadsheet ();
  var source = ss.getRange ("MySheet!A4:A10");

  source.copyTo (ss.getRange ("MySheet!B32"), {contentsOnly: true});

}

其中B32是B列中的第一个空单元格.

where B32 is the first empty cell in column B.

这完美地工作了,但是B32当然并不总是第一个空单元格,我需要动态获取该第一个空单元格.我通过合并在线找到的其他脚本尝试了一些事情,但到目前为止没有成功...

This works perfectly but of course B32 will not always be first empty cell and i need to get that first empty cell dynamically. I tried a few things by merging other scripts found online, but no success so far...

任何帮助将不胜感激! 谢谢!

Any help would be greatly appreciated! Thank you!

推荐答案

以下是一个函数,该函数将在给定的列和工作表中返回第一个空单元格(例如字符串,例如"B32"):

Here's a function that will return the first empty cell (as a string eg. 'B32') in a given column and sheet:

function getFirstEmptyCellInCol(sheet, column) {
  var values = sheet.getRange(column + ':' + column).getValues(); // get all the data of the column
  var rowCount = 0;
  while ( values[rowCount] && values[rowCount][0] != "" ) {
    rowCount++;
  }
  return column + parseInt(rowCount+1).toString();
}

您可以这样称呼它:

var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("MySheet");
emptyCell = getFirstEmptyCellInCol(sheet,'B');

使用您的代码:

function CopyValues() {
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("MySheet");
  var emptyCell = getFirstEmptyCellInCol(sheet,'B');
  var source = sheet.getRange ("A4:A10");
  source.copyTo (sheet.getRange(emptyCell), {contentsOnly: true});
}

这篇关于如何将列复制到另一列的第一个空白单元格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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