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
If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.
Drive.Files.insert()
.When your script is modified, please modify as follows. Before you run the script, please set the variables of destSpreadsheetId
and destSheetName
.
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);
If I misunderstood your question and this was not the direction you want, I apologize.