如果你有一个 URL,也许你可以通过这种方式获得一个 ID:
var url = 'https://docs.google.com/document/d/1BxECZrrIQpKFJYN5zHUeUDxeEiB8NjEb/edit'
var id = url.split('/')[5]
console.log(id) // output: 1BxECZrrIQpKFJYN5zHUeUDxeEiB8NjEb
然后您可以通过 ID 获取文件DriveApp
并将其转换为 Google doc。如何做到这一点的新例子在这里:
使用应用脚本将 MS Word 文件(保存在云端硬盘中)转换为 Google Docs
如果您有 .docx 文件的 URL(我从您的问题中不清楚),代码可能是这样的:
function main() {
var url = 'https://docs.google.com/document/d/abcd123456789/edit';
var id = url.split('/')[5];
var doc_file = convert_docx_to_google_doc(id);
var doc = DocumentApp.openById(doc_file.id);
var text = doc.getBody().getText(); // <--- contents of the doc file is here
console.log(text);
DriveApp.getFileById(doc_file.id).setTrashed(true); // delete the doc file
}
// you need to enable Advanced Drive API Service
function convert_docx_to_google_doc(id) {
var docx = DriveApp.getFileById(id);
var folder = docx.getParents().next();
var google_doc = Drive.Files
.insert(Drive.newFile(), docx.getBlob(), {convert:true});
// it saves the file next to the original one in the same folder
// it's not necessary if you don't need the file
DriveApp.getFileById(google_doc.id)
.setName(docx.getName().replace(/\.docx$/,'')).moveTo(folder);
return google_doc; // return the google doc file
}