Google Apps脚本 - 如何将列复制到下一个可用列的另一个工作表 [英] Google Apps Script - How to copy a column(s) to another sheet at next available column

查看:124
本文介绍了Google Apps脚本 - 如何将列复制到下一个可用列的另一个工作表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要求,那就是我需要从一列(或多列)中取值,并将它们复制到下一个可用列中的另一张纸上。我写过这样的脚本。它确实复制了一列中的值,但无法向前移动以获取同一源列中的新数据的另一个快照到目标工作表的下一空闲列。

  * //保留每月销售数字的副本
函数readSalesNum(){
var sheetFrom = SpreadsheetApp.getActiveSpreadsheet()。getSheetByName(Sales plan);
var sheetTo = SpreadsheetApp.getActiveSpreadsheet()。getSheetByName(SalesRecordsMonthly);

//从第17行第4列复制一列的所有行
var rangeToCopy = sheetFrom.getRange(17,4,sheetFrom.getMaxRows(),1);

//从第一个单元格开始粘贴到另一个表格
rangeToCopy.copyTo(sheetTo.getRange(1,1));
} *

对于不好的格式化我很抱歉:(我需要修改这个脚本从源表中提及任何一组列并将它们复制到目标表中;对于新的月份,它应该在下一组列中执行相同的操作,而不是像现在这样覆盖。我知道ContentOnly在脚本中有一个选项,但不知道如何使用它。

解决方案

如果我理解正确,这里是一个代码,它可以完成你想要的任务,即从第17行的第4列中的一个表中获取值,并将其复制到另一个表中,而不会覆盖从第1行开始的列

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

//从第17行,第4列,所有行复制一列
var valuesToCopy = sheetFrom.getRange(17,4,sheetFrom.getLastRow(),1).getValues();

//从第一个单元格开始粘贴到另一个表格
sheetTo.getRange(1,sheetTo.getLastColumn()+ 1,valuesToCopy.length,1).setValues(valuesToCopy);
}

测试工作表 here ,复制运行脚本 - 仅查看这仅涵盖问题的第一部分,第二部分有点令人困惑(正如我在上面的评论中提到的那样)。


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.

解决方案

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

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).

这篇关于Google Apps脚本 - 如何将列复制到下一个可用列的另一个工作表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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