בדף הזה של Google Docs API מוסבר איך לבצע משימות מסוימות ברמה גבוהה שקשורות למסמכי Google Docs, כמו:
- יצירת מסמך
- העתקת מסמך קיים
בקטעים הבאים מפורטות הפעולות האלה.
יצירת מסמך ריק
כדי ליצור מסמך, משתמשים ב-method documents.create
באוסף documents
.
בדוגמה הבאה של קוד אפשר לראות איך יוצרים מסמך ריק עם כותרת מוגדרת:
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:
אחרי שיוצרים את המסמך, מעבירים אותו לתיקייה ספציפית באמצעות השיטה
files.update
של Drive API. מידע נוסף על העברת קבצים זמין במאמר העברת קבצים בין תיקיות.מוסיפים מסמך ריק לתיקייה באמצעות method
files.create
של Drive API, ומציינים אתapplication/vnd.google-apps.document
כ-mimeType
. למידע נוסף על יצירת קבצים, אפשר לעיין במאמר יצירת קובץ בתיקייה ספציפית.
בכל אחת מהחלופות, צריך להוסיף את היקפי ההרשאות המתאימים של Drive API כדי לאשר את הקריאה. מידע נוסף על היקפי ההרשאות של Drive זמין במאמר בנושא בחירת היקפי הרשאות של Google Drive API.
כדי להעביר או ליצור קובץ בתיקייה באחסון השיתופי, אפשר לעיין במאמר בנושא הטמעה של תמיכה באחסון שיתופי.
העתקת מסמך קיים
כדי להעתיק מסמך, משתמשים ב-method files.copy
של Drive API.
בדוגמה הבאה לקוד מוצג איך להעתיק מסמך קיים. אפשר למצוא את המזהה שמשמש לקריאה ל-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.