יצירה וניהול של מסמכים

בדף הזה של Google Docs API מוסבר איך לבצע משימות מסוימות ברמה גבוהה שכוללים מסמכי Google Docs, כמו:

  • יצירת מסמך
  • העתקת מסמך קיים

הפסקאות הבאות מתארות את המשימות האלו בפירוט.

יצירת מסמך ריק

כדי ליצור מסמך, משתמשים ב השיטה documents.create אוסף documents.

דוגמת הקוד הבאה מראה איך ליצור מסמך ריק עם :title

Java

private static void createDoc(Docs service) throws IOException {
    Document doc = new Document()
            .setTitle("My Document");
    doc = service.documents().create(doc)
            .execute();
    System.out.println("Created document with title: " + doc.getTitle());
}

PHP

$title = 'My Document';
$document = new Google_Service_Docs_Document(array(
    'title' => $title
));

$document = $service->documents->create($document);
printf("Created document with title: %s\n", $document->title);

Python

title = 'My Document'
body = {
    'title': title
}
doc = service.documents() \
    .create(body=body).execute()
print('Created document with title: {0}'.format(
    doc.get('title')))

עבודה עם תיקיות Google Drive

אין אפשרות ליצור מסמך ישירות במסגרת תיקיית Drive באמצעות Docs API. כברירת מחדל, המסמך שנוצר נשמר בתיקיית הבסיס של המשתמש ב-Drive.

עם זאת, יש שתי חלופות לשמירת קובץ ב-Drive תיקייה:

בכל אחת מהאפשרויות האלה, צריך להוסיף את Drive API המתאים היקפי הרשאות לאישור את השיחה. למידע נוסף על היקפים ב-Drive, אפשר לעיין במאמר בחירת ההיקפים של Google Drive API

כדי להעביר או ליצור קובץ בתוך תיקייה באחסון השיתופי, כדאי לעיין במאמר הטמעה של קבצים משותפים תמיכה ב-Drive.

העתקת מסמך קיים

כדי להעתיק מסמך, צריך להשתמש ב-Drive API files.copy.

בדוגמה הבאה אפשר לראות איך מעתיקים מסמך קיים. טיפים נוספים לאופטימיזציה מפורטים המזהה שישמש לקריאה ל-Drive API בכתובת ה-URL של המסמך. לקבלת מידע נוסף ראו מזהה מסמך.

https://docs.google.com/document/d/DOCUMENT_ID/edit

Java

String copyTitle = "Copy Title";
File copyMetadata = new File().setName(copyTitle);
File documentCopyFile =
        driveService.files().copy(documentId, copyMetadata).execute();
String documentCopyId = documentCopyFile.getId();

Node.js

var copyTitle = "Copy Title";
let request = {
  name: copyTitle,
};
this.driveService.files.copy({
  fileId: documentId,
  resource: request,
}, (err, driveResponse) => {
  let documentCopyId = driveResponse.id;
});

PHP

<?php
$copyTitle = 'Copy Title';
$copy = new Google_Service_Drive_DriveFile(array(
    'name' => $copyTitle
));
$driveResponse = $driveService->files->copy($documentId, $copy);
$documentCopyId = $driveResponse->id;

Python

copy_title = 'Copy Title'
body = {
    'name': copy_title
}
drive_response = drive_service.files().copy(
    fileId=document_id, body=body).execute()
document_copy_id = drive_response.get('id')

לתשומת ליבכם: צריך להשתמש ב-Drive API מתאים היקף ההרשאות כדי לאשר שיחה. למידע נוסף על היקפים ב-Drive, אפשר לעיין במאמר בחירת ההיקפים של Google Drive API