将数据从一张表复制到另一张电子表格会排除特定列 [英] Copy data from one sheet to another spreadsheet exclude specific columns

查看:127
本文介绍了将数据从一张表复制到另一张电子表格会排除特定列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到了这个脚本,并且已经成功地将数据从主表复制到另一个名为煮熟的表单中。麻烦的是它复制了主表单中的所有数据,我只想复制特定列中的值,例如列A,C,D,F(除了B& E)并自动排列到目标表单。



我的编码exp是有限的,但我可以通过学习(复制)来了解它。)

  function copyTo(source,destination){
var sourceSheet = source.getSheet();
var destSheet = destination.getSheet();
var sourceData = source.getValues();
var dest = destSheet.getRange(
destination.getRow(),//目的地首行
destination.getColumn(),//目的地左上角
sourceData.length ,//源中的#行
sourceData [0] .length); //源中的元素col(第一行元素)
dest.setValues(sourceData);
SpreadsheetApp.flush();


函数copySheet(){
var sourceSheet = SpreadsheetApp.getActiveSpreadsheet()。getSheetByName(Total);
var destSheet = SpreadsheetApp.getActiveSpreadsheet()。getSheetByName(Cooked);

var source = sourceSheet.getRange(A:N);
var destination = destSheet.getRange(A:N);
copyTo(来源,目的地);


解决方案

c> copySheet 函数,你可以看到它需要 Range A:N 来进行复制。如果你改变这个部分,你可以复制所需的范围。



当你想复制一组不连续的列时,我会建议这样的。 >

 函数copySheet(){
var sourceSheet = SpreadsheetApp.getActiveSpreadsheet()。getSheetByName(Total);
var destSheet = SpreadsheetApp.getActiveSpreadsheet()。getSheetByName(Cooked);

var columns_to_be_copied = ['A','C','D','F'];
var columns_to_be_pasted = ['A','B','C','D'];

(column_to_be_copied中的列){

var copy_range_string = columns_to_be_copied [column] +':'+ columns_to_be_copied [column];
var paste_range_string = columns_to_be_pasted [column] +':'+ columns_to_be_pasted [column];

var source = sourceSheet.getRange(copy_range_string);
var destination = destSheet.getRange(paste_range_string);
copyTo(来源,目的地);






$ b我们将所有要复制的列存储在一个变量中,遍历它们并生成 A:A copy_range_strings paste_range_strings $ c>, C:C 等。然后在这些范围内调用copyTo方法。现在您可以编辑 columns_to_be_copied columns_to_be_pasted 以更改从哪一列到哪一列的数据。


I found the script and have successfully copy values from master sheet to another sheet named "Cooked". The trouble is it copies all the data from master sheet, I just want to copy values from specific columns, like column A,C,D,F(Except B&E) and auto arrange to destination sheet.

My coding exp is limited but I'm able to learn(copy) to understand it from you :)

function copyTo(source,destination) {
  var sourceSheet = source.getSheet();
  var destSheet = destination.getSheet();
  var sourceData = source.getValues();
  var dest = destSheet.getRange(
    destination.getRow(),        // Top row of destination
    destination.getColumn(),     // left col of destination
    sourceData.length,           // # rows in source
    sourceData[0].length);       // # cols in source (elements in first row)
  dest.setValues(sourceData);
  SpreadsheetApp.flush();
}

function copySheet() {
   var sourceSheet  = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Total");
   var destSheet  = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Cooked");

  var source = sourceSheet.getRange("A:N");
  var destination  = destSheet.getRange("A:N");
  copyTo(source,destination );
}

解决方案

In your copySheet function, you can see it takes Range A:N for copying. If you change this part, you can copy desired ranges.

As you wanna copy a set of non-contiguous columns, I'd suggest something like this.

function copySheet() {
  var sourceSheet  = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Total");
  var destSheet  = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Cooked");

  var columns_to_be_copied = ['A', 'C', 'D', 'F'];
  var columns_to_be_pasted = ['A', 'B', 'C', 'D'];

  for (column in columns_to_be_copied) {

    var copy_range_string = columns_to_be_copied[column] + ':' + columns_to_be_copied[column];
    var paste_range_string = columns_to_be_pasted[column] + ':' + columns_to_be_pasted[column];

    var source = sourceSheet.getRange(copy_range_string);
    var destination  = destSheet.getRange(paste_range_string);
    copyTo(source,destination );
  }
}

We store all columns to be copied in a variable, iterate through them and generate copy_range_strings and paste_range_strings of the formant A:A, C:C etc. Then call copyTo method on those ranges. Now you can edit columns_to_be_copied and columns_to_be_pasted to make changes on data from which column should go to which column

这篇关于将数据从一张表复制到另一张电子表格会排除特定列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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