简体   繁体   English

电子表格可复制其他电子表格中的信息-Javascript

[英]Spreadsheet to copy informations from another sheets - Javascript

I'm looking way to create easier version of my code. 我正在寻找一种方法来创建更容易的代码版本。 I want to, copy information from every sheet to one sheet. 我想将信息从每张纸复制到一张纸上。 First informations from first sheet, then below from second sheet etc. Now I have every each code for sheet, it is any way to make it easier? 首先是第一张纸的信息,然后是第二张纸的下面的信息。等等。现在,我有了每张纸的每个代码,有什么方法可以使它更容易? What code should use? 应该使用什么代码? Below my code and drawing with explanation. 在我的代码和图纸下面并附有解释。

 function copy() { var ur = SpreadsheetApp.getActiveSpreadsheet(); // sheet where we copy data var urgents = ur.getSheetByName("List"); var lastRow = urgents.getLastRow(); var range = urgents.getRange("A2:B"); range.clear(); // every time script clear data var importsh = SpreadsheetApp.openById('115nqlMpW3dhI-adpWvir5RZeayM01HqvjOcDc2_yLvQ'); var imbrak = importsh.getSheetByName('List_1'); // sheet from we copy data var imbrak2 = importsh.getSheetByName('List_2'); // sheet from we copy data var lastcol = importsh.getLastColumn(); var lastcol2 = imbrak2.getLastColumn(); var last = imbrak.getLastRow(); var last2 = imbrak2.getLastRow(); var urlast = last; var urlast2 = last2; var data = imbrak.getRange("A2:B"+last).getValues(); var data2 = imbrak2.getRange("A2:B"+last2).getValues(); urgents.getRange("A2:B"+last).setValues(data); urgents.getRange(getFirstEmptyRowWholeRow3(),1,last2-1,2).setValues(data2); } 

在此处输入图片说明

Regards 问候

How about this modification? 这个修改怎么样? Please think of this as one of several answers. 请认为这是几个答案之一。

Modification points : 修改要点:

  • Use an array for sheet names. 为工作表名称使用数组。
    • By this, when it increases the number of sheets, you can use this script by importing the sheet names to the array. 这样,当它增加工作表数量时,可以通过将工作表名称导入数组来使用此脚本。
  • All data of each sheets is imported to an array. 每张纸的所有数据均导入到数组中。
    • By this, the data can be imported to the destination sheet by one call of setValues() . 这样,可以通过一次调用setValues()将数据导入到目标表中。

Modified script : 修改后的脚本:

function copy() {
  var ur = SpreadsheetApp.getActiveSpreadsheet(); // sheet where we copy data
  var urgents = ur.getSheetByName("List"); 
  var lastRow = urgents.getLastRow();
  var range = urgents.getRange("A2:B");
  range.clear(); // every time script clear data

  // Added
  var importsh = SpreadsheetApp.openById('115nqlMpW3dhI-adpWvir5RZeayM01HqvjOcDc2_yLvQ');
  var sheets = ['List_1', 'List_2']; // Please import sheet names here.
  var data = [];
  sheets.forEach(function(e){
    var s = importsh.getSheetByName(e);
    var values = s.getRange("A2:B"+s.getLastRow()).getValues();
    Array.prototype.push.apply(data, values);
  });
  urgents.getRange(2, 1, data.length, data[0].length).setValues(data); // Please confirm this range.
}

Note : 注意 :

  • Please confirm the range of urgents.getRange(2, 1, data.length, data[0].length).setValues(data); 请确认urgents.getRange(2, 1, data.length, data[0].length).setValues(data); . Because I have no information about getFirstEmptyRowWholeRow3() . 因为我没有关于getFirstEmptyRowWholeRow3()
    • In the current range, the data is imported to "A2:B". 在当前范围内,数据将导入到“ A2:B”。

If I misunderstand your question, please tell me. 如果我误解了您的问题,请告诉我。 I would like to modify. 我要修改。

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

相关问题 将多个工作表从一个电子表格复制到另一个电子表格 - Copy several sheets from one spreadsheet to another spreadsheet 在编辑时将数据从一个电子表格复制到另一个电子表格 - Copy data from one spreadsheet to another on edit Javascript从按钮上传信息 - Javascript Upload informations from button Google Sheets Api:复制谷歌电子表格 - Google Sheets Api: Copy google spreadsheet 将一行从一个Google表格电子表格复制到表单提交中的另一表格 - Copy a row from one Google Sheet Spreadsheet to Another on Form Submit 如何在编辑时将数据从一个电子表格复制到另一个电子表格 - How to copy data from one spreadsheet to another on edit 将数据从一个电子表格复制到另一种保留格式且不使用公式 - Copy data from one Spreadsheet to another keeping format and without formulas 将过滤后的范围从一个电子表格复制到另一个 - Google 应用脚本 - Copy filtered range from one spreadsheet to another - Google app script 仅将值从多个选项卡复制到另一个电子表格的脚本 - Script to copy values only from multiple tabs to another spreadsheet Google脚本会根据日期将范围从一个电子表格复制到另一个电子表格 - Google scripts copy range from one spreadsheet to another based on date
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM