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

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

问题描述

当然我已经解决了这个问题,但就是做对了.有一个脚本设置为从一张纸中获取数据并将其放入另一张纸中,我已经这样做了,但是在复制时会留下空白,我无法弄清楚如何解决它.我确定在代码中,我打了问号的地方就是问题所在.

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();
}

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

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