Javascript 使用google drive api上载excel文件
我对google drive API有问题 我正在尝试使用此API上载excel文件,但它不起作用。即使复制GoogleAPI文档也不起作用 以下是我的代码示例:Javascript 使用google drive api上载excel文件,javascript,node.js,excel,google-api,google-drive-api,Javascript,Node.js,Excel,Google Api,Google Drive Api,我对google drive API有问题 我正在尝试使用此API上载excel文件,但它不起作用。即使复制GoogleAPI文档也不起作用 以下是我的代码示例: @Get('teste') async teste(){ const keys = require(path.resolve('src', 'files', 'api', 'keys')) const client = new google.auth.JWT( keys.client_email,
@Get('teste')
async teste(){
const keys = require(path.resolve('src', 'files', 'api', 'keys'))
const client = new google.auth.JWT(
keys.client_email,
null,
keys.private_key,
['https://www.googleapis.com/auth/drive.metadata.readonly']
)
client.authorize((err, tokens) =>{
if(err){
console.log(err)
return;
} else{
this.gdrun(client)
}
})
}
gdrun(client){
const drive = google.drive({version: 'v3', auth: client});
var fileMetadata = {
name: 'My Report',
mimeType: 'application/vnd.google-apps.spreadsheet'
};
var media = {
mimeType: 'application/vnd.ms-excel',
body: require(path.resolve('src', 'files', 'excel', 'solargroup.xlsx'))
};
drive.files.create({
resource: fileMetadata,
media: media,
fields: 'id'
}, function (err, file: any) {
if (err) {
// Handle error
console.error(err);
} else {
console.log('File Id:', file.id);
}
});
}
我收到了这个错误:
我相信你的目标如下
gdrun(client){
const drive = google.drive({ version: "v3", auth: client });
var fileMetadata = {
name: "My Report",
mimeType: "application/vnd.google-apps.spreadsheet",
};
var media = {
mimeType: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", // Modified
body: fs.createReadStream(path.resolve('src', 'files', 'excel', 'solargroup.xlsx')), // Modified
};
drive.files.create(
{
resource: fileMetadata,
media: media,
fields: "id",
},
function (err, file) {
if (err) {
console.error(err);
} else {
console.log("File Id:", file.data.id); // Modified
}
}
);
}
var fileMetadata = {
name: "My Report",
mimeType: "application/vnd.google-apps.spreadsheet",
parents: ["### folderId ###"], // Please set the folder ID of the folder shared with the service account.
};
- 您想将一个文件(XLSX文件)上载到服务帐户的Google驱动器
- 您希望使用带有googleapis for Node.js的服务帐户来实现这一点
- 根据您的脚本,我认为您可能希望通过转换将XLSX文件上载为Google电子表格
- 如果您想将文件上载到Google Drive,在这种情况下,请使用
而不是https://www.googleapis.com/auth/drive
https://www.googleapis.com/auth/drive.metadata.readonly
- 当您希望将XLSX文件作为XLSX文件上载时,mimeType为
application/vnd.openxmlformats of icedocument.spreadsheetml.sheet
- 当您要上载文件时,
无法使用。在这种情况下,请使用body:require(path.resolve('src','files','excel','solargroup.xlsx'))
。我想你的错误信息可能是由于这个原因body:fs.createReadStream(path.resolve('src','files','excel','solargroup.xlsx'))
- 当您要检索上载文件的文件ID时,请将
修改为file.ID
file.data.ID
gdrun(client){
const drive = google.drive({ version: "v3", auth: client });
var fileMetadata = {
name: "My Report",
mimeType: "application/vnd.google-apps.spreadsheet",
};
var media = {
mimeType: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", // Modified
body: fs.createReadStream(path.resolve('src', 'files', 'excel', 'solargroup.xlsx')), // Modified
};
drive.files.create(
{
resource: fileMetadata,
media: media,
fields: "id",
},
function (err, file) {
if (err) {
console.error(err);
} else {
console.log("File Id:", file.data.id); // Modified
}
}
);
}
var fileMetadata = {
name: "My Report",
mimeType: "application/vnd.google-apps.spreadsheet",
parents: ["### folderId ###"], // Please set the folder ID of the folder shared with the service account.
};
修改脚本:
发件人:
致:
另外,请修改您的gdrun()
,如下所示
gdrun(client){
const drive = google.drive({ version: "v3", auth: client });
var fileMetadata = {
name: "My Report",
mimeType: "application/vnd.google-apps.spreadsheet",
};
var media = {
mimeType: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", // Modified
body: fs.createReadStream(path.resolve('src', 'files', 'excel', 'solargroup.xlsx')), // Modified
};
drive.files.create(
{
resource: fileMetadata,
media: media,
fields: "id",
},
function (err, file) {
if (err) {
console.error(err);
} else {
console.log("File Id:", file.data.id); // Modified
}
}
);
}
var fileMetadata = {
name: "My Report",
mimeType: "application/vnd.google-apps.spreadsheet",
parents: ["### folderId ###"], // Please set the folder ID of the folder shared with the service account.
};
- 在这种情况下,请使用
const fs=require(“fs”)
File Id: ###fileId###
注:
- 您的脚本将XLSX文件作为Google电子表格上传到Google Drive of service帐户。在这种情况下,您无法在Google Drive上直接看到上载的文件。因为服务帐户的Google驱动器与您的Google驱动器不同。当你想在谷歌硬盘上看到上传的文件时,请在谷歌硬盘上创建一个文件夹,并将创建的文件夹与服务帐户的电子邮件共享。然后,请将文件上载到共享文件夹。这样,你就可以用浏览器在谷歌硬盘上看到上传的文件。为此,请修改
,如下所示fileMetadata
gdrun(client){ const drive = google.drive({ version: "v3", auth: client }); var fileMetadata = { name: "My Report", mimeType: "application/vnd.google-apps.spreadsheet", }; var media = { mimeType: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", // Modified body: fs.createReadStream(path.resolve('src', 'files', 'excel', 'solargroup.xlsx')), // Modified }; drive.files.create( { resource: fileMetadata, media: media, fields: "id", }, function (err, file) { if (err) { console.error(err); } else { console.log("File Id:", file.data.id); // Modified } } ); }
var fileMetadata = { name: "My Report", mimeType: "application/vnd.google-apps.spreadsheet", parents: ["### folderId ###"], // Please set the folder ID of the folder shared with the service account. };
- 在上面的脚本中,最大文件大小为5 MB。请小心这个。当您想上传超过5 MB的文件时,请使用可恢复上传
- 我相信你的目标如下
gdrun(client){
const drive = google.drive({ version: "v3", auth: client });
var fileMetadata = {
name: "My Report",
mimeType: "application/vnd.google-apps.spreadsheet",
};
var media = {
mimeType: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", // Modified
body: fs.createReadStream(path.resolve('src', 'files', 'excel', 'solargroup.xlsx')), // Modified
};
drive.files.create(
{
resource: fileMetadata,
media: media,
fields: "id",
},
function (err, file) {
if (err) {
console.error(err);
} else {
console.log("File Id:", file.data.id); // Modified
}
}
);
}
var fileMetadata = {
name: "My Report",
mimeType: "application/vnd.google-apps.spreadsheet",
parents: ["### folderId ###"], // Please set the folder ID of the folder shared with the service account.
};
- 您想将一个文件(XLSX文件)上载到服务帐户的Google驱动器
- 您希望使用带有googleapis for Node.js的服务帐户来实现这一点
- 根据您的脚本,我认为您可能希望通过转换将XLSX文件上载为Google电子表格
- 如果您想将文件上载到Google Drive,在这种情况下,请使用
而不是https://www.googleapis.com/auth/drive
https://www.googleapis.com/auth/drive.metadata.readonly
- 当您希望将XLSX文件作为XLSX文件上载时,mimeType为
application/vnd.openxmlformats of icedocument.spreadsheetml.sheet
- 当您要上载文件时,
无法使用。在这种情况下,请使用body:require(path.resolve('src','files','excel','solargroup.xlsx'))
。我想你的错误信息可能是由于这个原因body:fs.createReadStream(path.resolve('src','files','excel','solargroup.xlsx'))
- 当您要检索上载文件的文件ID时,请将
修改为file.ID
file.data.ID
gdrun(client){
const drive = google.drive({ version: "v3", auth: client });
var fileMetadata = {
name: "My Report",
mimeType: "application/vnd.google-apps.spreadsheet",
};
var media = {
mimeType: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", // Modified
body: fs.createReadStream(path.resolve('src', 'files', 'excel', 'solargroup.xlsx')), // Modified
};
drive.files.create(
{
resource: fileMetadata,
media: media,
fields: "id",
},
function (err, file) {
if (err) {
console.error(err);
} else {
console.log("File Id:", file.data.id); // Modified
}
}
);
}
var fileMetadata = {
name: "My Report",
mimeType: "application/vnd.google-apps.spreadsheet",
parents: ["### folderId ###"], // Please set the folder ID of the folder shared with the service account.
};
修改脚本:
发件人:
致:
另外,请修改您的gdrun()
,如下所示
gdrun(client){
const drive = google.drive({ version: "v3", auth: client });
var fileMetadata = {
name: "My Report",
mimeType: "application/vnd.google-apps.spreadsheet",
};
var media = {
mimeType: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", // Modified
body: fs.createReadStream(path.resolve('src', 'files', 'excel', 'solargroup.xlsx')), // Modified
};
drive.files.create(
{
resource: fileMetadata,
media: media,
fields: "id",
},
function (err, file) {
if (err) {
console.error(err);
} else {
console.log("File Id:", file.data.id); // Modified
}
}
);
}
var fileMetadata = {
name: "My Report",
mimeType: "application/vnd.google-apps.spreadsheet",
parents: ["### folderId ###"], // Please set the folder ID of the folder shared with the service account.
};
- 在这种情况下,请使用
const fs=require(“fs”)
File Id: ###fileId###
注:
- 您的脚本将XLSX文件作为Google电子表格上传到Google Drive of service帐户。在这种情况下,您无法在Google Drive上直接看到上载的文件。因为服务帐户的Google驱动器与您的Google驱动器不同。当你想在谷歌硬盘上看到上传的文件时,请在谷歌硬盘上创建一个文件夹,并将创建的文件夹与服务帐户的电子邮件共享。然后,请将文件上载到共享文件夹。这样,你就可以用浏览器在谷歌硬盘上看到上传的文件。为此,请修改
,如下所示fileMetadata
gdrun(client){ const drive = google.drive({ version: "v3", auth: client }); var fileMetadata = { name: "My Report", mimeType: "application/vnd.google-apps.spreadsheet", }; var media = { mimeType: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", // Modified body: fs.createReadStream(path.resolve('src', 'files', 'excel', 'solargroup.xlsx')), // Modified }; drive.files.create( { resource: fileMetadata, media: media, fields: "id", }, function (err, file) { if (err) { console.error(err); } else { console.log("File Id:", file.data.id); // Modified } } ); }
var fileMetadata = { name: "My Report", mimeType: "application/vnd.google-apps.spreadsheet", parents: ["### folderId ###"], // Please set the folder ID of the folder shared with the service account. };
- 在上面的脚本中,最大文件大小为5 MB。请小心这个。当您想上传超过5 MB的文件时,请使用可恢复上传