[英]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.