简体   繁体   中英

Import Multiple Spreadsheets into 1 Sheet using Google Apps Script on Google Sheets

Hi I try to run a code found on the internet in app scripts

function addMenu()
{
 var menu = SpreadsheetApp.getUi().createMenu('Custom');
 menu.addItem('Copy Data', 'getData');
 menu.addToUi();
}

function onOpen(e)
{
 addMenu(); 
}

function getData() {
  
  get_files = ['SpreadSheet Example 1', 'SpreadSheet Example 2'];
  
  var ssa = SpreadsheetApp.getActiveSpreadsheet();
  var copySheet = ssa.getSheetByName('DATA');
  copySheet.getRange('A2:Z').clear();  
  
  for(z = 0; z < get_files.length; z++)
  {
  
    var files = DriveApp.getFilesByName(get_files[z]);
    while (files.hasNext()) 
    {
      var file = files.next();
      break;
    }
  
    var ss = SpreadsheetApp.open(file);
    SpreadsheetApp.setActiveSpreadsheet(ss);
    var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
    
    for(var i = 0; i < sheets.length; i++)
    {  
      var nameSheet = ss.getSheetByName(sheets[i].getName()); 
      var nameRange = nameSheet.getDataRange();
      var nameValues = nameRange.getValues();
      
      for(var y = 1; y < nameValues.length; y++)
      {
        copySheet.appendRow(nameValues[y]); 
      }     
    }    
  }  
}

I get this error

Exception: Service Spreadsheets failed while accessing document with id 1YdIDE2OgJrtHPvCGA5c. (This I get when I add the .xlsx to file names in the string) eg:SpreadSheet Example 1.xlsx

And I get this when I delete the extension

Exception: Argument cannot be null: file

The files are in the same folder in the google drive Can you show me where should I look

Modification points:

  • From your error message, I'm worried that the files of get_files might include the files except for Google Spreadsheet.
  • In your script, appendRow is used in a loop. In this case, the process cost will become high. Ref

If you want to achieve your goal using the Google Spreadsheet service, how about the following modification?

Modified script:

function getData() {
  var get_files = ['SpreadSheet Example 1', 'SpreadSheet Example 2'];
  var ssa = SpreadsheetApp.getActiveSpreadsheet();
  var copySheet = ssa.getSheetByName('DATA');
  copySheet.getRange('A2:Z').clear();
  for (z = 0; z < get_files.length; z++) {
    var files = DriveApp.searchFiles(`title='${get_files[z]}' and mimeType='${MimeType.GOOGLE_SHEETS}' and trashed=false`);
    var file = files.hasNext() && files.next();
    if (!file) {
      console.log(`"${get_files[z]}" of Google Spreadsheet was not found.`);
      continue;
    }
    var sheets = SpreadsheetApp.open(file).getSheets();
    for (var i = 0; i < sheets.length; i++) {
      var values = sheets[i].getDataRange().getValues();
      copySheet.getRange(copySheet.getLastRow() + 1, 1, values.length, values[0].length).setValues(values);
    }
  }
}
  • When this script is run, Google Spreadsheet files are retrieved from the files of get_files . And, the values of each sheet are appended to copySheet .

References:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM