简体   繁体   English

仅将值从多个选项卡复制到另一个电子表格的脚本

[英]Script to copy values only from multiple tabs to another spreadsheet

I have found so many scripts to copy values only from one spreadhseet to another.我发现很多脚本只能将值从一个电子表格复制到另一个电子表格。 However, all of them is to copy the whole spreasheet.但是,所有这些都是复制整个电子表格。

I am very new with google script and cannot find a way to copy values only from specific tabs to another spreasheet adding these new tabs to it.我对谷歌脚本非常陌生,无法找到一种方法将值仅从特定选项卡复制到另一个添加这些新选项卡的电子表格。

function temp() {

  var sss = SpreadsheetApp.openById('XYZ'); // sss = source spreadsheet
  //var ss = sss.getSheets()[4]; // ss = source sheet

  var ss = sss.getSheets(); // ss = source sheet
  var id=4; //default number

  for(var i in ss)
  {
    var sheet = ss[i];
    if(sheet.getName()== "ABC")
    {  id=i;
      break;
    }
  }
  console.log(id);
  
  ss=sss.getSheets()[id];

  //Get full range of data
  var SRange = ss.getDataRange();
  //get A1 notation identifying the range
  var A1Range = SRange.getA1Notation();
  //get the data values in range
  var SData = SRange.getValues();
  SpreadsheetApp.flush();
  var tss = SpreadsheetApp.getActiveSpreadsheet(); // tss = target spreadsheet
  var ts = tss.getSheetByName('ABC'); // ts = target sheet
  //set the target range to the values of the source data
  ts.getRange(A1Range).setValues(SData);


}

Thanks a lot in advance.非常感谢。

Copy these sheets to another spreadsheet将这些工作表复制到另一个电子表格

function copythese() {
  const ss = SpreadsheetApp.getActive();
  const these = ['Sheet0','Sheet1'];//Put your sheetnames here
  const another = SpreadsheetApp.openById("another id");//put the other spreadsheet id here
  ss.getSheets().filter(sh => ~these.indexOf(sh.getName())).forEach(sh => {
    sh.copyTo(another);
  });
}

Append These Values to another spreadsheet Append 这些值到另一个电子表格

function appendthesevalues() {
  const ss = SpreadsheetApp.getActive();
  const these = ['Sheet0','Sheet1'];
  const another = SpreadsheetApp.openById("another id");
  ss.getSheets().filter(sh => ~these.indexOf(sh.getName())).forEach(sh => {
    let vs = sh.getDataRange().getValues();
    let nsh = another.getSheetByName(sh.getName());
    if(!nsh) {
      nsh = another.insertSheet(sh.getName());
      SpreadsheetApp.flush();
    }
    if(nsh) {
      nsh.getRange(nsh.getLastRow() + 1, 1 ,vs.length, vs[0].length).setValues(vs);
    }
  });
}

Issue:问题:

  • You can to copy the values from a specific set of sheets to a new spreadsheet.您可以将一组特定工作表中的值复制到新的电子表格中。
  • I assume that you don't want to copy the sheet itself, but only the values from the source sheet.我假设您不想复制工作表本身,而只想复制源工作表中的值。

Modifications:修改:

  • Use filter to get an array of your desired sheets (based on sheet name).使用过滤器获取所需工作表的数组(基于工作表名称)。
  • If the target spreadsheet doesn't have a sheet with that name, create it with Spreadsheet.insertSheet .如果目标电子表格没有同名的工作表,请使用Spreadsheet.insertSheet创建它。

Code sample:代码示例:

function temp() {
  var sheetNames = ["ABC", "DEF"]; // Change accordingly
  var sss = SpreadsheetApp.openById('XYZ');
  var sheetsToCopy = sss.getSheets().filter(s => sheetNames.includes(s.getSheetName()));
  sheetsToCopy.forEach(ss => {
    var sourceSheetName = ss.getSheetName();
    var SRange = ss.getDataRange();
    var A1Range = SRange.getA1Notation();
    var SData = SRange.getValues();
    var tss = SpreadsheetApp.getActiveSpreadsheet();
    var ts = tss.getSheetByName(sourceSheetName);
    if (!ts) ts = tss.insertSheet(sourceSheetName);
    ts.getRange(A1Range).setValues(SData);    
  });
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用应用程序脚本将值从一个子文件夹中的某些电子表格复制到另一电子表格 - How can I copy values from some spreadsheets that are in one subfolder to another spreadsheet with app script 将过滤后的范围从一个电子表格复制到另一个 - Google 应用脚本 - Copy filtered range from one spreadsheet to another - Google app script Google电子表格将值从一行复制到另一张工作表 - Google spreadsheet copy values from one row to another sheet 如何使用谷歌应用脚本将一行从一个谷歌电子表格复制到另一个谷歌电子表格? - How to copy a row from one google spreadsheet to another google spreadsheet using google apps script? 如何使用Google Apps脚本将特定的行从3个Google电子表格复制到另一个电子表格 - How to copy specific rows from 3 google spreadsheet to another spreadsheet using google apps script 将工作表复制到另一个电子表格[Google Apps脚本] - Copy sheet to another spreadsheet [Google Apps Script] Google电子表格在多个单元格中显示脚本中的值 - google spreadsheet display values from script in multiple cells Google 工作表脚本将数据集从一张工作表复制到另一张工作表的顶部(仅限值) - Google sheets script copy data sets from one sheet to another (values only) at the top of the sheet 电子表格可复制其他电子表格中的信息-Javascript - Spreadsheet to copy informations from another sheets - Javascript 将多个工作表从一个电子表格复制到另一个电子表格 - Copy several sheets from one spreadsheet to another spreadsheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM