Google apps script 将列值复制到另一个google电子表格文件而不覆盖

Google apps script 将列值复制到另一个google电子表格文件而不覆盖,google-apps-script,google-sheets,google-sheets-api,Google Apps Script,Google Sheets,Google Sheets Api,我有下面的场景。 公式电子表格中的某些列应按顺序复制,而不将旧内容覆盖到主电子表格文件中 我成功地合并了以下脚本 function copy() { var ss = SpreadsheetApp.getActive(), target =ss.getSheetByName('Sheet2'), //fill in the name of the sheet lastColumn = target.getLastColumn(), sourceVal = ss.ge

我有下面的场景。 公式电子表格中的某些列应按顺序复制,而不将旧内容覆盖到主电子表格文件中

我成功地合并了以下脚本

function copy() {

var ss = SpreadsheetApp.getActive(),
    target =ss.getSheetByName('Sheet2'),   //fill in the name of the sheet
    lastColumn = target.getLastColumn(),
    sourceVal = ss.getSheetByName('Sheet1').getRange("C1:C58")
        .getValues();
target.getRange(1, lastColumn + 1, sourceVal.length, sourceVal[0].length)
    .setValues(sourceVal);

var ss = SpreadsheetApp.getActive(),
    target =ss.getSheetByName('Sheet2'),   //fill in the name of the sheet
    lastColumn = target.getLastColumn(),
    sourceVal = ss.getSheetByName('Sheet1').getRange("H1:H58")
        .getValues();
target.getRange(1, lastColumn + 1, sourceVal.length, sourceVal[0].length)
    .setValues(sourceVal);
}
这将适用于所需数量的列,并且不会覆盖旧信息

不幸的是,我不明白如何使这与其他电子表格文件的工作

最终结果应该从10个文件(每个文件有2列)读取到最终的主文件中(列不是连续的)

提前感谢您抽出时间。

这对我很有用:

function copy() {
  var sources = [
    {id: '1XTdXQjcy48D63Yl-fHFOvCocZo5jWN10d_fYfhf5umo', // ID of first source file
     sheet1: 'Sheet1',
     range1: 'C1:C58',
     sheet2: 'Sheet1',
     range2: 'H1:H58'
    },
    {id: '12wpXOlkKI2Zb8n5op4xuWQrWn7fyiH8j49NV8kB2b9s',
     sheet1: 'Sheet1',
     range1: 'C1:C58',
     sheet2: 'Sheet1',
     range2: 'H1:H58'
    } // add next sheets
   ]
  sources.forEach(function(val) {
    val.val1 = SpreadsheetApp.openById(val.id).getSheetByName(val.sheet1).getRange(val.range1).getValues();
    val.val2 = SpreadsheetApp.openById(val.id).getSheetByName(val.sheet2).getRange(val.range2).getValues();
    return val
  })

  var target = SpreadsheetApp.getActive().getSheetByName('Sheet2')

  for (var i=0; i<sources.length; i++) {
    var lastColumn = target.getLastColumn()
    target.getRange(1, lastColumn + 1, sources[i].val1.length, sources[i].val1[0].length).setValues(sources[i].val1);

    var lastColumn = target.getLastColumn()
    target.getRange(1, lastColumn + 1, sources[i].val2.length, sources[i].val2[0].length).setValues(sources[i].val2);
  }
}
函数复制(){
风险值来源=[
{id:'1XTdXQjcy48D63Yl-fHFOvCocZo5jWN10d_fYfhf5umo',//第一个源文件的id
第1张:“第1张”,
范围1:'C1:C58',
第2张:“第1张”,
范围2:'H1:H58'
},
{id:'12wpXOlkKI2Zb8n5op4xuWQrWn7fyiH8j49NV8kB2b9s',
第1张:“第1张”,
范围1:'C1:C58',
第2张:“第1张”,
范围2:'H1:H58'
}//添加下一页
]
sources.forEach(函数(val){
val.val1=SpreadsheetApp.openById(val.id).getSheetByName(val.sheet1.getRange(val.range1.getValues();
val.val2=SpreadsheetApp.openById(val.id).getSheetByName(val.sheet2).getRange(val.range2).getValues();
返回值
})
var target=SpreadsheetApp.getActive().getSheetByName('Sheet2'))
对于(var i=0;i这对我很有效:

function copy() {
  var sources = [
    {id: '1XTdXQjcy48D63Yl-fHFOvCocZo5jWN10d_fYfhf5umo', // ID of first source file
     sheet1: 'Sheet1',
     range1: 'C1:C58',
     sheet2: 'Sheet1',
     range2: 'H1:H58'
    },
    {id: '12wpXOlkKI2Zb8n5op4xuWQrWn7fyiH8j49NV8kB2b9s',
     sheet1: 'Sheet1',
     range1: 'C1:C58',
     sheet2: 'Sheet1',
     range2: 'H1:H58'
    } // add next sheets
   ]
  sources.forEach(function(val) {
    val.val1 = SpreadsheetApp.openById(val.id).getSheetByName(val.sheet1).getRange(val.range1).getValues();
    val.val2 = SpreadsheetApp.openById(val.id).getSheetByName(val.sheet2).getRange(val.range2).getValues();
    return val
  })

  var target = SpreadsheetApp.getActive().getSheetByName('Sheet2')

  for (var i=0; i<sources.length; i++) {
    var lastColumn = target.getLastColumn()
    target.getRange(1, lastColumn + 1, sources[i].val1.length, sources[i].val1[0].length).setValues(sources[i].val1);

    var lastColumn = target.getLastColumn()
    target.getRange(1, lastColumn + 1, sources[i].val2.length, sources[i].val2[0].length).setValues(sources[i].val2);
  }
}
函数复制(){
风险值来源=[
{id:'1XTdXQjcy48D63Yl-fHFOvCocZo5jWN10d_fYfhf5umo',//第一个源文件的id
第1张:“第1张”,
范围1:'C1:C58',
第2张:“第1张”,
范围2:'H1:H58'
},
{id:'12wpXOlkKI2Zb8n5op4xuWQrWn7fyiH8j49NV8kB2b9s',
第1张:“第1张”,
范围1:'C1:C58',
第2张:“第1张”,
范围2:'H1:H58'
}//添加下一页
]
sources.forEach(函数(val){
val.val1=SpreadsheetApp.openById(val.id).getSheetByName(val.sheet1.getRange(val.range1.getValues();
val.val2=SpreadsheetApp.openById(val.id).getSheetByName(val.sheet2).getRange(val.range2).getValues();
返回值
})
var target=SpreadsheetApp.getActive().getSheetByName('Sheet2'))
对于(var i=0;i尝试使用openById(id):

  • 打开具有给定ID的电子表格
代码示例:

 // The code below opens a spreadsheet using its ID and logs the name for it.
 // Note that the spreadsheet is NOT physically opened on the client side.
 // It is opened on the server only (for modification by the script).
 var ss = SpreadsheetApp.openById("abc1234567");
 Logger.log(ss.getName());
要访问不同的工作表并将其插入主文件,请使用
openById(id)

希望这有帮助。

尝试使用openById(id):

  • 打开具有给定ID的电子表格
代码示例:

 // The code below opens a spreadsheet using its ID and logs the name for it.
 // Note that the spreadsheet is NOT physically opened on the client side.
 // It is opened on the server only (for modification by the script).
 var ss = SpreadsheetApp.openById("abc1234567");
 Logger.log(ss.getName());
要访问不同的工作表并将其插入主文件,请使用
openById(id)


希望这能有所帮助。

这真是太棒了。我(不知怎么的)理解它,并且能够根据要求进行更改。非常感谢。这真是太棒了。我(不知怎么的)理解它,并且能够根据要求进行更改。非常感谢。