在Google应用程序脚本中将数据从一张表复制到另一张表中,并追加一行,这是一个小问题 [英] copy data from one sheet to another in google app script and append a row, one small issue

查看:89
本文介绍了在Google应用程序脚本中将数据从一张表复制到另一张表中,并追加一行,这是一个小问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当然,我有这个失败,但不能正确的。有一个脚本设置为从一张纸上获取数据并将其放入另一张,这一点我已经完成,但在复印时会留下空白,我无法弄清楚如何解决这个问题。我确信在代码中,它的位置是问号所在。

sure I had this down but just can't get it right. Had a script set up to get data from one sheet and put it into another one, which I have done but it leaves gaps when copying and I can't figure out how to solve it. I'm sure in the code, its where I have put a question mark, is where the problem lies.

我试过把i,last,last + 1, 10,12但这些工作都没有,觉得我错过了一些小东西来得到这个权利。如果需要,以下代码是查看工作表的链接(该工作表仅供我学习,如果您愿意的话,也是一个基本示例)。

I have tried put i, last, last+1, 10, 12 but none of these work, feel like i'm missing something small to get this right. Below is the code a link to view the sheet if needed (the sheet is just for me to learn from, a basic example if you will).

预先感谢如果代码可以写的更好,请让我知道仍然在学习:)

Thanks in advance and also if the code could be better written, please just let me know as still learning this :)

function copyInfo() {
  var app = SpreadsheetApp;
  var copySheet = app.getActiveSpreadsheet().getSheetByName("Copy");
  for (var i = 2; i <12; i++) {   
  var getInfo = copySheet.getRange(2,2,i,2).getValues();
  //  get the info from range above - start at row 2 on column 2 (b), get number of rows i , number of columns = 2, b,c 
  var last = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Paste").getLastRow();
  var pasteSheet = app.getActiveSpreadsheet().getSheetByName("Paste");
//  Tell it where you want the info to go to 

  pasteSheet.getRange(last+1,1,?,2).setValues(getInfo);
  var clearIt = copySheet.getRange(2,2,i,2).clearContent();  
// this clears the copy range aka getInfo
  }}

链接到表格

推荐答案

您可以使用 copyTo ,所以你的函数可以改写为:

You can copy whole range at once using copyTo, so your function could be rewritten as:

function copyInfo() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var copySheet = ss.getSheetByName("Copy");
  var pasteSheet = ss.getSheetByName("Paste");

  // get source range
  var source = copySheet.getRange(2,2,12,2);
  // get destination range
  var destination = pasteSheet.getRange(pasteSheet.getLastRow()+1,2,12,2);

  // copy values to destination range
  source.copyTo(destination);

  // clear source values
  source.clearContent();
}

这篇关于在Google应用程序脚本中将数据从一张表复制到另一张表中,并追加一行,这是一个小问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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