简体   繁体   English

Google Apps脚本-如何将列复制到下一个可用列的另一个工作表

[英]Google Apps Script - How to copy a column(s) to another sheet at next available column

I have a requirement where I need to take values from one column (or more) and copy them to another sheet in the next available column (s). 我有一个要求,我需要从一个(或多个)列中获取值,然后将其复制到下一个可用列中的另一张表中。 I have written a script like this. 我写了这样的脚本。 It does copy the values from one column but it has no way to move forward for taking another snapshot of new data in same source column to destination sheet's next free column. 它确实从一列中复制值,但是无法继续前进以将同一源列中的新数据的另一个快照复制到目标工作表的下一个空闲列中。

*//keep a copy of Sales numbers for every month
function readSalesNum() {
var sheetFrom = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sales plan");
var sheetTo = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("SalesRecordsMonthly");

// Copy from 17th row, 4th column, all rows for one column 
var rangeToCopy = sheetFrom.getRange(17, 4, sheetFrom.getMaxRows(), 1);

//Paste to another sheet from first cell onwards
rangeToCopy.copyTo(sheetTo.getRange(1, 1));
}*

I am sorry about the poor formatting :( I need to modify this script to mention any set of columns from source sheet and copy them to destination sheet. And for new month, it should do the same in next set of columns, instead of overwriting as it does now. Also, the copy should happen only for values which is missing yet. I know there's an option of ContentOnly in script but not sure how to use it. 我为格式不佳而感到抱歉:(我需要修改此脚本以提及源工作表中的任何列集,并将它们复制到目标工作表中。对于新的月份,它应该在下一组列中执行相同的操作,而不是覆盖就像现在一样。而且,复制仅应针对尚不存在的值进行。我知道脚本中有ContentOnly选项,但不确定如何使用。

If I understood correctly, here is a code that does what you wanted, ie get the values from one sheet in column4 from row 17 and copy it to the other sheet without overwriting to columns starting at row 1 如果我理解正确的话,下面的代码可以满足您的要求,即从第17行的column4的一张工作表中获取值,然后将其复制到另一张工作表中,而不会覆盖从第1行开始的列

function readSalesNum() {
var sheetFrom = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sales plan");
var sheetTo = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("SalesRecordsMonthly");

// Copy from 17th row, 4th column, all rows for one column 
var valuesToCopy = sheetFrom.getRange(17, 4, sheetFrom.getLastRow(), 1).getValues();

//Paste to another sheet from first cell onwards
sheetTo.getRange(1,sheetTo.getLastColumn()+1,valuesToCopy.length,1).setValues(valuesToCopy);
}

test sheet here , make a copy to run the script - view only 测试页在这里只视图- ,进行复印运行脚本

This covers only the first part of your question, the second part was a bit confusing (as mentioned in my comment above). 这仅涵盖了您问题的第一部分,第二部分有些令人困惑(如我在上面的评论中所述)。

Try this 尝试这个

*//keep a copy of Sales numbers for every month
function readSalesNum() {
  var sheetFrom = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sales plan");
  var sheetTo = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("SalesRecordsMonthly");

  // Copy from 17th row, 4th column, all rows for one column 
  var rangeValues = sheetFrom.getRange(17, 4, 1, sheetFrom.getMaxRows()).getValues();

  //Paste to another sheet from first cell onwards

  sheetTo.appendRow(rangeValues[0]);

}

This will do what you want with the range you suggested. 这将根据您建议的范围来做您想要的。 However, looks like you're trying to get two different ranges so you'll have to accommodate that. 但是,您似乎要尝试获得两个不同的范围,因此必须适应这一范围。

暂无
暂无

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

相关问题 Google Sheet Apps 脚本:如何编辑下面的 Apps 脚本以仅将数据从 A 列复制到 L 以及从 P 复制到 S? - Google Sheet Apps Script: How do I edit the Apps Script below to only copy over data from Column A to L and P to S? 如何使用Google Apps脚本匹配一列并替换另一张工作表中的另一列(对应)? - How to match one column and replace a different (corresponding) column in another sheet using google apps script? Google Apps脚本在一个工作表中复制一列,将其转置,然后粘贴到另一个 - Google Apps Script Copying a column in one sheet, transposing it, then pasting to another 将工作表复制到另一个电子表格[Google Apps脚本] - Copy sheet to another spreadsheet [Google Apps Script] 如何使用 Google Apps 脚本在列中搜索任何值并将行移动到另一个工作表? - How can I search a column for any value and move the row to another sheet using Google Apps Script? 谷歌表应用程序脚本截断列值 - google sheet apps script truncate column value Google Apps 脚本 - 格式化特定工作表中的列 - Google Apps Script - Format Column in Specific Sheet Google Apps脚本 - 将包含今天日期的一张表格中的行复制到另一张表格中 - Google Apps Script - Copy rows from one sheet containing today's date into another 要使用Google Apps脚本将单元格文本和注释复制到另一个工作表? - To copy the cell text and the comments to another sheet using google apps script? 如何检查列 header 是否存在,如果不存在则添加 header 列 - Google Sheet Apps Script - How to check if column header exists and if not add header column - Google Sheet Apps Script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM