Reputation: 253
Below code creates a google sheet on My Drive but what if I had to place this to a folder on team drive?
I would like to modify createNewSpreadSheet function in such a way that it uploads file to specified folder of team drive.
function createNewSpreadSheet(auth) {
const sheets = google.sheets({version: 'v4', auth});
const resource = {
properties: {
title:'SampleSheet'
},
};
sheets.spreadsheets.create({
resource,
fields: 'spreadsheetId',
}, (err, spreadsheet) =>{
if (err) {
// Handle error.
console.log(err);
} else {
console.log('spreadsheet::',spreadsheet.data.spreadsheetId);
}
});
}
// Load client secrets from a local file.
fs.readFile('credentials.json', (err, content) => {
if (err) return console.log('Error loading client secret file:', err);
// Authorize a client with credentials, then call the Google Sheets API.
authorize(JSON.parse(content), createNewSpreadSheet);
});
Upvotes: 0
Views: 640
Reputation: 13494
You may check this blog on how to import/upload files to Team Drives folders.
Uploading files to a Team Drives folder is also identical to to uploading to a normal Drive folder, and also done with
DRIVE.files().create()
. Importing is slightly different than uploading because you're uploading a file and converting it to a G Suite/Google Apps document format, i.e., uploading CSV as a Google Sheet, or plain text or Microsoft Word® file as Google Docs. In the sample app, we tackle the former:def import_csv_to_td_folder(folder_id, fn, mimeType): body = {'name': fn, 'mimeType': mimeType, 'parents': [folder_id]} return DRIVE.files().create(body=body, media_body=fn+'.csv', supportsTeamDrives=True, fields='id').execute().get('id')
The secret to importing is the MIMEtype. That tells Drive whether you want conversion to a G Suite/Google Apps format (or not). The same is true for exporting. The import and export MIMEtypes supported by the Google Drive API can be found in my SO answer here.
Upvotes: 1