Vishal Borana
Vishal Borana

Reputation: 37

Export Angular table to google sheet

I have a requirement where I want to export my Table data from my angular app to google sheets. I'm able to export it to csv format and download it but I'm not able to open it in google sheets.

Following is the code that downloads the csv.

downloadDadeFile(data, filename = 'Project Database') {
    let csvData = this.ConvertToCSVDADE(data, ['deal_id', 'project_name', 'manager_name', 'manager_id', 'operational_unit', 'project_applicability', 'reason', 'qassure_flag', 'comments']);
    let blob = new Blob(['\ufeff' + csvData], { type: 'text/csv;charset=utf-8;' });
    let dwldLink = document.createElement("a");
    let url = URL.createObjectURL(blob);
    let isSafariBrowser = navigator.userAgent.indexOf('Safari') != -1 && navigator.userAgent.indexOf('Chrome') == -1;
    if (isSafariBrowser) {  //if Safari open in new window to save file with random filename.
      dwldLink.setAttribute("target", "_blank");
    }
    debugger
    dwldLink.setAttribute("href", url);
    dwldLink.setAttribute("id", 'a');
    dwldLink.setAttribute("download", filename + ".csv");
    var a = document.getElementsByTagName("a");
    window.open("https://docs.google.com/spreadsheets/" + a[0].getAttribute("value"), "_blank")
    dwldLink.style.visibility = "hidden";
    document.body.appendChild(dwldLink);
    dwldLink.click();
    document.body.removeChild(dwldLink);
  } 

Can Someone kindly point me in the right direction to open this in Google sheets rather than downloading it?

Upvotes: 0

Views: 1054

Answers (2)

Aerials
Aerials

Reputation: 4419

Once you have the .csv file, you should use the Drive API to upload the file. If you want the file to be opened in Google Sheets editor,then convert it upon uploading.

Follow this documentation.

Example using Node.js:

var fileMetadata = {
  'name': 'My Report',
  'mimeType': 'application/vnd.google-apps.spreadsheet'
};
var media = {
  mimeType: 'text/csv',
  body: fs.createReadStream('files/report.csv')
};
drive.files.create({
  resource: fileMetadata,
  media: media,
  fields: 'id'
}, function (err, file) {
  if (err) {
    // Handle error
    console.error(err);
  } else {
    console.log('File Id:', file.id);
  }
});

Upvotes: 1

Timothy Alexis Vass
Timothy Alexis Vass

Reputation: 2705

In Google sheets, you can use =IMPORTDATA("blob:<uri_of_your_blob>")

Upvotes: 0

Related Questions