Google 工作表,将多个来源的数据导入一个,并创建新表

Google Sheets, Import data from multiple sources into one, and create new tables

The source data that i want to import, its on a different multiple worksheets 此图像包含我要发送到主 table 的所有数据, Two of the tables are filled in manually, i want to do the third one using a script entirely

我已经弄清楚如何使用此代码将数据从一个 sheet 写入另一个:

function getdata() {
  var files = DriveApp.getFolderById("folder id").getFiles()
    while (files.hasNext()) {
      var file = files.next();
      var shoot = SpreadsheetApp.openById(file.getId());
      
      var sourcesheet = SpreadsheetApp.getActive().getSheetByName("Sheet name");;
      var sourcerange = sourcesheet.getRange('A:A');
      var sourcevalues = sourcerange.getValues();
      
    var destsheet = shoot.getSheetByName('Sheet name'); 
    var destrange = destsheet.getRange('B:B'); 
    destrange.setValues(sourcevalues);         
 }
 }

问题在于它不会为新数据创建新行,并且不会考虑何时创建新 sheet。 我有点困惑,我是 javascript 的新手,我只是 IT 人员,但我愿意学习!

example

答案:

看起来你想使用 Range class 的 copyTo() 方法。

更多信息:

根据文档:

copyTo(destination)

Copies the data from a range of cells to another range of cells. Both the values and formatting are copied.

// The code below copies the first 5 columns over to the 6th column.
var sheet = SpreadsheetApp.getActiveSheet();
var rangeToCopy = sheet.getRange(1, 1, sheet.getMaxRows(), 5);
rangeToCopy.copyTo(sheet.getRange(1, 6));

Parameters | Name | Type | Description | |------|------|-------------| |destination | Range | A destination range to copy to; only the top-left cell position is relevant.| Authorization

Scripts that use this method require authorization with one or more of the following scopes:

  • https://www.googleapis.com/auth/spreadsheets.currentonly
  • https://www.googleapis.com/auth/spreadsheets

工作示例:

使用如下脚本:

function myFunction() {
  SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1").getRange("C6:D20").copyTo(
    SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet2").getRange("E6:F20")
  )
}

您可以将数据从一个范围复制到另一个范围,同时保留文本修饰等属性:

参考文献: