【问题标题】:How to merge multiple spreadsheet into one master spreadsheet?如何将多个电子表格合并为一个主电子表格?
【发布时间】:2022-01-27 14:13:20
【问题描述】:

我有多个 Google 电子表格,我想将它们合并到一个主文件中。我从这里找到了不同的参考,但到目前为止,我的案例没有任何效果。这是我做的尝试代码:

function compileSheets() {
    
  var dataSourceWorkbook = SpreadsheetApp.getActive()
    
  //Open Sheet "Link List" and pull the data inside the script
  //Sample of link source can be found here: spreadsheetID 1Nua_Lhcnjec8w34hnL8kZsdeDqqkggqo63pY_pxaRy0
    
  var linkSourceSheet = dataSourceWorkbook.getSheetByName('Links')
  var linkSource = linkSourceSheet.getDataRange().getDisplayValues() //this contain links of spreadsheets that I want to compile
    
  //compile file --> to restore all the multiple spreadhseet into one
  var compilefile = SpreadsheetApp.openById('12zj2-wlBXi6Rd18nUQMiB-dY-3xz10IQLxeehNPlXeQ') //I provide a sample spreadsheet for reference
  var compilefilesheet = compilefile.getSheetByName('Compiled')
      
  var compiledData = []
    
  for(row in linkSource){ 
  //I create a case, if the report status is 'Updated' I will compile the spreadsheet
    
  if (linkSource[row][3]=="Report Updated") {
        
 //open report by URL
 var report = SpreadsheetApp.openByUrl(linkSource[row][2])
    
 //get 2d list
 var updatedreport = report.getSheetByName('Sheet1')
 var reportvalues = updatedreport.getRange(2, 1, updatedreport.getLastRow(), 16).getValues()

 //merge multiple 2d list into one
 compiledData.concat(reportvalues)
    
          
  }
}
    
  //print to compile spreadsheet
  compilefilesheet.clear()
    
  if(compiledData.length){
  compilefilesheet.getRange(2, 1, compiledData.length, compiledData[0].length).setValues(compiledData);
    
        
   }
      
 }

上面代码的问题,我已经成功拉取了二维列表,然而:

  1. 我无法删除空白行,因为每个报告都有空白行。
  2. 使用 concat 合并数据失败
  3. 将数据打印到编译表时失败

你们知道如何解决这个问题吗? 提前谢谢!

供参考: 链接到链接源:link 编译表链接:link

【问题讨论】:

    标签: google-apps-script google-sheets


    【解决方案1】:

    在你的脚本中,下面的修改怎么样?

    发件人:

    compiledData.concat(reportvalues)
    

    收件人:

    compiledData = compiledData.concat(reportvalues);
    

    compiledData = [...compiledData, ...reportvalues];
    

    参考:

    附加信息:

    作为附加信息,当您想删除空行时,您也可以进行如下修改。

    发件人:

    compiledData.concat(reportvalues)
    

    收件人:

    compiledData = compiledData.concat(reportvalues.filter(r => r.join("") != ""));
    

    compiledData = [...compiledData, ...reportvalues.filter(r => r.join("") != "")];
    

    【讨论】:

    • 谢谢!现在它的工作和打印非常好:) 但是我想知道,你知道如何删除每个文件中的空白行吗?当我拉出二维列表时,仍然包含空白行
    • @Nadila 感谢您的回复。我添加了一个修改后的脚本来删除空行。你能确认一下吗?
    • 它现在可以工作了,虽然我只使用其中一个代码,因为当我尝试同时放置两个代码时,它正在复制数据。谢谢你!
    • @Nadila 感谢您的回复。是的。请使用其中之一。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-06
    • 1970-01-01
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多