How do I copy data from an xlsx file in my google drive to a google sheet?

前端 未结 1 1061
无人共我
无人共我 2021-01-23 18:26

I have an excel file (.xlsx) saved on my google drive. I want to copy the data from there into a tab in a google sheet file i have already created. I have the following code tha

相关标签:
1条回答
  • 2021-01-23 18:47
    • You want to copy the values of ".xlsx" file to the existing Google Spreadsheet.
    • There is the ".xlsx" file in your Google Drive.
    • You want to achieve this using Google Apps Script.

    If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.

    Modification points:

    • In order to retrieve the values from the Google Spreadsheet converted from the ".xlsx" file, it uses the file ID returned from Drive.Files.insert().
    • In your script, it is required to declare the destination Google Spreadsheet which copies the values from the ".xlsx" file and to use it.

    Modified script:

    When your script is modified, please modify as follows. Before you run the script, please set the variables of destSpreadsheetId and destSheetName.

    From:
    fileName = fileName || "G:\Shared drives\ExchangeData\DailyVolumes.xlsx";
    
    var excelFile = DriveApp.getFilesByName(fileName).next();
    var fileId = excelFile.getId();
    var folderId = Drive.Files.get(fileId).parents[0].id;  
    var blob = excelFile.getBlob();
    var resource = {
      title: excelFile.getName().replace(/.xlsx?/, ""),
      key: fileId
    };
    Drive.Files.insert(resource, blob, {
      convert: true
    });
    
    To:
    var destSpreadsheetId = "###";  // Added
    var destSheetName = "###";  // Added
    
    fileName = fileName || "G:\Shared drives\ExchangeData\DailyVolumes.xlsx";
    var excelFile = DriveApp.getFilesByName(fileName).next();
    var fileId = excelFile.getId();
    // var folderId = Drive.Files.get(fileId).parents[0].id; // This is not used in your script.
    var blob = excelFile.getBlob();
    var resource = {title: excelFile.getName().replace(/.xlsx?/, "")};  // Modified
    var sourceSpreadsheet = Drive.Files.insert(resource, blob, {convert: true});  // Modified
    
    // Also I added below script.
    var sourceSheet = SpreadsheetApp.openById(sourceSpreadsheet.id).getSheets()[0];
    var destSheet = SpreadsheetApp.openById(destSpreadsheetId).getSheetByName(destSheetName);
    var values = sourceSheet.getDataRange().getValues();
    destSheet.getRange(destSheet.getLastRow() + 1, 1, values.length, values[0].length).setValues(values);
    

    Note:

    • In your question, I'm not sure whether the ".xlsx" file has several sheets and the range you want to put the values. So in this modification, as a sample, the values of the 1st tab in the converted Spreadsheet are copied to the last row in the destination Spreadsheet.
    • In this modified script, it supposes that you have already enabled Drive API at Advanced Google services.

    References:

    • Files: insert
    • openById(id)
    • getSheets()
    • getDataRange()
    • getValues()
    • setValues(values)

    If I misunderstood your question and this was not the direction you want, I apologize.

    0 讨论(0)
提交回复
热议问题