本文目录导读:

- Google Docs (Google Apps Script)
- Microsoft Office (VBA)
- Python (处理通用文档)
- JavaScript (浏览器环境)
- Git (版本控制)
- Linux/Unix 系统文件
- 注意事项:
Google Docs (Google Apps Script)
function getDocumentCreator() {
// 获取当前文档
var doc = DocumentApp.getActiveDocument();
// 获取文档ID
var fileId = doc.getId();
// 使用Drive API获取文档详细信息
var file = DriveApp.getFileById(fileId);
// 获取创建者的电子邮件地址
var creatorEmail = file.getOwner().getEmail();
// 如果需要更详细的创建信息,使用Drive API
var driveFile = Drive.Files.get(fileId, {
fields: 'createdBy, modifiedBy, createdDate'
});
return {
creatorEmail: creatorEmail,
createdBy: driveFile.createdBy,
createdDate: driveFile.createdDate
};
}
Microsoft Office (VBA)
Sub GetDocumentCreator()
' 获取文档创建者信息
Dim props As DocumentProperty
' 方法1:使用内置属性
Debug.Print "Author: " & ThisDocument.BuiltInDocumentProperties("Author")
Debug.Print "Created by: " & ThisDocument.Author
' 方法2:使用自定义属性
Set props = ThisDocument.CustomDocumentProperties("Creator")
If Not props Is Nothing Then
Debug.Print "Creator: " & props.Value
End If
' 方法3:获取文件的元数据
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Dim file As Object
Set file = fso.GetFile(ThisDocument.FullName)
Debug.Print "File owner: " & file.DateCreated
End Sub
Python (处理通用文档)
import os
from datetime import datetime
# 对于PDF文件
import PyPDF2
def get_pdf_creator(filepath):
with open(filepath, 'rb') as file:
pdf = PyPDF2.PdfFileReader(file)
info = pdf.getDocumentInfo()
return info.auther if hasattr(info, 'auther') else info.author
# 对于Word文档(.docx)
from docx import Document
def get_docx_creator(filepath):
doc = Document(filepath)
core_props = doc.core_properties
return core_props.author
# 对于Excel文件
import openpyxl
def get_xlsx_creator(filepath):
wb = openpyxl.load_workbook(filepath)
return wb.properties.creator
# 通用方法:获取文件系统元数据
def get_file_creator_metadata(filepath):
return {
'creator': os.popen(f'powershell.exe -Command "(Get-Item \'{filepath}\').GetAccessControl().Owner"').read().strip(),
'created': datetime.fromtimestamp(os.path.getctime(filepath))
}
JavaScript (浏览器环境)
// 如果是Office Online文档
async function getOfficeDocCreator() {
try {
// 使用Office JS API
await Office.onReady();
if (Office.context && Office.context.document) {
const doc = Office.context.document;
// 获取文档属性
doc.getFilePropertiesAsync(function(result) {
if (result.status === "succeeded") {
const props = result.value;
console.log("Created by:", props.author);
console.log("Created date:", props.created);
}
});
}
} catch (error) {
console.error("Error getting document creator:", error);
}
}
Git (版本控制)
# 查看文件创建作者(如果是Git仓库中的文件) git log --format='%an' --diff-filter=A -- "文件名" | tail -1 # 或者用脚本方式 #!/bin/bash file=$1 author=$(git log --format='%an' --diff-filter=A -- "$file" | tail -1) date_created=$(git log --format='%ad' --date=short --diff-filter=A -- "$file" | tail -1) echo "Creator: $author" echo "Created: $date_created"
Linux/Unix 系统文件
#!/bin/bash
# 获取文件创建者(使用stat命令)
file="$1"
creator=$(stat -c '%U' "$file")
creation_time=$(stat -c '%W' "$file" 2>/dev/null || stat -c '%Y' "$file")
echo "文件所有者: $creator"
echo "文件创建时间: $(date -d @$creation_time)"
# 或使用Python
python3 -c "
import os, pwd, time
file_path = '$1'
st = os.stat(file_path)
creator = pwd.getpwuid(st.st_uid).pw_name
print(f'文件创建者: {creator}')
"
注意事项:
- 不同平台和文档类型获取创建者的方式不同
- 某些文档格式(如纯文本)可能不包含创建者信息
- 获取创建者信息可能需要相应权限
- 文件名中的路径和权限可能影响结果
选择适合你使用的文档类型和平台的方案即可,如果你需要特定的文档类型,请告诉我具体是什么类型的文档。