首页 文章

将范围从一个电子表格复制到另一个电子表格

提问于
浏览
3

我试图从一个工作表复制数组的内容(通过迭代创建数组并将用户选择的列的选择项推送到另一个工作表中的不同工作表) .

我已经遇到了一些关于如何将范围从一个电子表格导入到另一个电子表格的问题和答案,但它们都没有对我有用(所有都返回错误“我们很抱歉,发生了服务器错误 . 请稍等一下然后再试一次 . “)我也试过'copyTo',没有成功 .

这是我目前使用的代码 . 我哪里出错?

function copyToTrix(featureList) {

  featureList = featureList.getValues();

  var tss = SpreadsheetApp.openById("0AtX9IYFZ..."); //define active trix as target 

var ts = tss.getSheetByName("US");          //define sheet in target trix

var newRange = ts.getRange("DA12:DA25");    //define target range

 newRange.setValues(featureList);  //set values in target range to items in featureList

Browser.msgBox('copied');
}

5 回答

  • 0

    我使用此函数来复制文档之间的整个工作表(仅限值):

    function updateSourceToTarget(sourceID,sourceName,targetID,targetname){
      Logger.log(sourceID + ' ' + sourceName + ' ' +targetname);
    var source = SpreadsheetApp.openById(sourceID).getSheetByName(sourceName);
    var destination = SpreadsheetApp.openById(targetID).getSheetByName(targetname);
    var sourcelastRow = source.getLastRow();
    var sourcelastCol = source.getLastColumn();
    var sourcedata = source.getRange(1,1,sourcelastRow,sourcelastCol).getValues();
    destination.getRange(1,1,sourcelastRow,sourcelastCol).setValues(sourcedata);
    }
    

    然后我调用这个函数,例如:

    updateSourceToTarget('sourceID','sourceSheetName','targetID','targetSheetName');
    

    在另一个文档中获取原始工作表中的数据副本 . 对于小文档非常有用,如果格式和函数不重要,则必须保持原始不受影响和非共享 .

    另一个方向是函数 sheet.copyto() ,它创建工作表的新副本,但是您需要一些不同的垃圾处理功能 . 祝好运!

  • -2

    您以一种非常危险的方式定义了目标范围,因为它只依赖于您的决定而不依赖于数组内容......您可以使用此方法始终有效:

    var beginning_row = 12;
    var beginning_col = /*what ever corresponds to 'DA' */ ;
    var newRange = ts.getRange(beginning_row, beginning_col, featureList.length, featureList[0].length);    //define target range
    

    它需要从数组长度(实际上是它)的行数和从其第一个元素的长度(相同的注释)的列数...非常简单和可靠;-)

    另外:你给自己的答案是错的(对不起,没有个人冒犯......),因为它导致与the doc的'best practice'章节中描述的内容完全相反,而我'd recommend to remove its '回答了标记'和upvote .

  • 1

    我在这里使用某人,我的代码对我有用 .

    function CopyRange() {
     var sss = SpreadsheetApp.openById('spreadsheetid'); //replace with source ID
     var ss = sss.getSheetByName('sheetname'); //replace with source Sheet tab name
     var range = ss.getRange('A2:G50'); //assign the range you want to copy
     var data = range.getValues();
    
     var tss = SpreadsheetApp.openById('SpreadsheetID'); //replace with destination ID
     var ts = tss.getSheetByName('sheetname2'); //replace with destination Sheet tab name
    
     ts.getRange(ts.getLastRow()+1, 1,49,7).setValues(data); //you will need to define the size of the copied data see getRange()
    
    }
    

    此代码获取范围A2:G50并复制到lastrow中的sheetname2 . 例如,如果sheet2的第一行是availble un row11,则此代码将在Row11中粘贴信息 .

  • 4

    copyTo和setValues都工作 - 我在我的代码中使用它们 .

    我会找到产生错误的行 - 日志,或尝试和捕获 .

    或添加这样的东西 - 看看它是否给你一些线索

    // The code below will set the values for range A1:D2 to the values in an array.
    var myTable = [[1, 2, 3, 4],[5, 6, 7, 8]];
    var sheet = SpreadsheetApp.getActiveSheet();
    sheet.getRange(1,1,2,4).setValues(myTable);
    
  • 0

    事实证明,GAS在使用setValues()函数时遇到了问题 . 这不是一个完整的范围,所以我最终不得不使用setValue() . (设置值仅允许您一次在一个单元格中操作):

    var tss = SpreadsheetApp.openById("0AtX9IYFZ9KkbdHg4TUdqYVppTTU5OENpb04tWTdNbHc");
    var ts = tss.getSheetByName("US");
    for (var i = 0; i < featureList.length; i++) {
    var position = i + 12;
    ts.setActiveCell("B" + position).setValue(featureList[i]);
    

相关问题