复制一个电子表格上的完整列表以附加到另一个电子表格的底部 [英] Copy a complete list on one spreadsheet to append on the bottom of another spreadsheet

查看:164
本文介绍了复制一个电子表格上的完整列表以附加到另一个电子表格的底部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Google Spreadsheet中,我只想从一个电子表格的完整列表中取值,并将其附加到另一个电子表格的列表底部。我的麻烦是,使用 copyValuesToRange()函数错误如下:

 目标工作表和源范围必须位于同一电子表格中。 

以下是我目前的代码:

  function transferList(){
var source = SpreadsheetApp.getActiveSpreadsheet();
var target = SpreadsheetApp.openById(0ABCD);
var target_sheet = target.getSheetByName(RFPData);
var sheet = source.getSheetByName(RFP List);
var sheet_last_row = sheet.getLastRow()+ 1;
var source_range = sheet.getRange(A2:I+ sheet_last_row);
var sWidth = source_range.getWidth()+ 1;
var sHeight = source_range.getHeight()+ 1;
var last_row = target_sheet.getLastRow();
source_range.copyValuesToRange(target_sheet,1,sWidth,
last_row + 1,last_row + sHeight);
}

任何想法如何让这工作?

解决方案

正如您发现的, copyValuesToRange()一个工作表对象 Spreadsheet Object 作为范围。没有一种原子方法会将一系列值复制到另一个电子表格中,但有很多方法可以做到这一点。



以下是一种方法。


  • 从源代码表中,通过使用 getDataRange选择完整数据范围,获取一次操作中的所有数据(),然后用 getValues()将所有值抓取到一个javascript数组中。

  • 第一行标题,请使用 JavaScript数组方法 splice()
  • 在目标工作表上找到您的目标,它位于当前存在的最后一行数据之后,使用 getLastRow()

  • 将源数据(无标题)写入下一行开始的目标工作表, setValues()



脚本:

  function transferList(){
var sourceSheet = SpreadsheetApp.getActiveSpreadsheet()。getSheetByName(RFP List);
var sourceData = sourceSheet.getDataRange()。getValues();
sourceData.splice(0,1); //删除标题
var targetSS = SpreadsheetApp.openById(0ABCD)。getSheetByName(RFPData);
var targetRangeTop = targetSS.getLastRow(); //获取#行当前在目标中
targetSS.getRange(targetRangeTop + 1,1,sourceData.length,sourceData [0] .length).setValues(sourceData);
}


In Google Spreadsheet I would like to take only the values from a complete list on one spreadsheet and append it to the bottom of a list on another spreadsheet. My trouble is that using the the copyValuesToRange() function errors the following:

Target sheet and source range must be on the same spreadsheet.

Here's my current code:

function transferList() {
  var source = SpreadsheetApp.getActiveSpreadsheet();
  var target = SpreadsheetApp.openById("0ABCD"); 
  var target_sheet = target.getSheetByName("RFPData");
  var sheet = source.getSheetByName("RFP List");
  var sheet_last_row = sheet.getLastRow() + 1;
  var source_range = sheet.getRange("A2:I"+sheet_last_row);
  var sWidth=source_range.getWidth() + 1;
  var sHeight=source_range.getHeight() + 1;
  var last_row=target_sheet.getLastRow();
  source_range.copyValuesToRange(target_sheet , 1, sWidth, 
    last_row + 1, last_row + sHeight );
}

Any idea how I can get this to work?

解决方案

As you've found, copyValuesToRange() is a Range method that affects a Sheet Object that is in the same Spreadsheet Object as the Range. There isn't an atomic method that will copy a range of values to another Spreadsheet, but there are a number of ways you could do it.

Here's one way.

  • From the source sheet, get all the data in one operation, by selecting the complete range of data using getDataRange() and then grabbing all values into a javascript array with getValues().
  • To ignore the first row of headers, use the javascript array method splice().
  • Locate your destination, which is on the target sheet, starting after the last row of data that's currently there, using getLastRow().
  • Write the source data (without the headers) to the destination sheet starting at the next row, using setValues().

Script:

function transferList() {
  var sourceSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("RFP List");
  var sourceData = sourceSheet.getDataRange().getValues();
  sourceData.splice(0,1);  // Remove header
  var targetSS = SpreadsheetApp.openById("0ABCD").getSheetByName("RFPData");
  var targetRangeTop = targetSS.getLastRow(); // Get # rows currently in target
  targetSS.getRange(targetRangeTop+1,1,sourceData.length,sourceData[0].length).setValues(sourceData);
}

这篇关于复制一个电子表格上的完整列表以附加到另一个电子表格的底部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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