הוספה, מחיקה והעברה של טקסט

ב-Google Docs API אפשר להוסיף או למחוק טקסט במסמך. הטקסט זז כוללת את שתי הפעולות, ולפניהן get של התוכן.

אפשר להוסיף או למחוק טקסט בכל אחד מהמסמך הפלחים (גוף, כותרת עליונה, כותרת תחתונה או הערת שוליים).

הוספת טקסט

כדי להוסיף טקסט למסמך, משתמשים ב documents.batchUpdate ונכלול InsertTextRequest עם הטקסט והמיקום כמטען הייעודי (Payload).

דוגמת הקוד הבאה ממחישה איך אפשר להוסיף סדרה של מחרוזות טקסט מיקומי אינדקס שצוינו בגוף המסמך. בדוגמה נעשה שימוש בשלושה קיזוז יעד (25, 50 ו-75) ומוסיפה מחרוזת בת עשרה תווים בכל פעם המיקום.

Java

List<Request> requests = new ArrayList<>();
requests.add(new Request().setInsertText(new InsertTextRequest()
        .setText(text1)
        .setLocation(new Location().setIndex(25).setTabId(TAB_ID))));

requests.add(new Request().setInsertText(new InsertTextRequest()
        .setText(text2)
        .setLocation(new Location().setIndex(50).setTabId(TAB_ID))));

requests.add(new Request().setInsertText(new InsertTextRequest()
        .setText(text3)
        .setLocation(new Location().setIndex(75).setTabId(TAB_ID))));

BatchUpdateDocumentRequest body = new BatchUpdateDocumentRequest().setRequests(requests);
BatchUpdateDocumentResponse response = docsService.documents()
        .batchUpdate(DOCUMENT_ID, body).execute();

PHP

$requests = array();
$requests[] = new Google_Service_Docs_Request(array(
    'insertText' => array(
        'text' => $text1,
        'location' => array(
            'index' => 25,
            'tabId' => TAB_ID,
        ),
    ),
    'insertText' => array(
        'text' => $text2,
        'location' => array(
            'index' => 50,
            'tabId' => TAB_ID,
        ),
    ),
    'insertText' => array(
        'text' => $text3,
        'location' => array(
            'index' => 75,
            'tabId' => TAB_ID,
        ),
    ),
));

$batchUpdateRequest = new Google_Service_Docs_BatchUpdateDocumentRequest(array(
    'requests' => $requests
));

$response = $service->documents->batchUpdate($documentId, $batchUpdateRequest);

Python

requests = [
        {
        'insertText': {
            'location': {
                'index': 25,
                'tabId': TAB_ID
            },
            'text': text1
        }
    },
                {
        'insertText': {
            'location': {
                'index': 50,
                'tabId': TAB_ID
            },
            'text': text2
        }
    },
                {
        'insertText': {
            'location': {
                'index': 75,
                'tabId': TAB_ID
            },
            'text': text3
        }
    },
]

result = service.documents().batchUpdate(
    documentId=DOCUMENT_ID, body={'requests': requests}).execute()

כל הזנה מגדילה את כל האינדקסים בעלי המספר הגבוה יותר לפי גודל טקסט שהוכנס. הדוגמה מחשבת מראש את התוצאה של השינויים באינדקס, כך שהוספות הבאות מתרחשות בקיזוז החדש והמתוקן. לכן צריך להוסיף בקיזוזי היעד המקורי של 25, 50 ו-75, מדדי ההכנסה בפועל הן:

  • ההכנסה הראשונה מוסיפה 10 תווים בהיסט 25.
  • בהכנסה השנייה מתווספים 10 תווים בהיסט של 50+10=60.
  • ההזנה השלישית מוסיפה 10 תווים בהיסט 75+10+10=95.

מחיקת הטקסט

כדי למחוק טקסט ממסמך, קודם בונים קובץ Range שמגדיר את הטווח של טקסט למחיקה. אחר כך משתמשים בשיטה documents.batchUpdate וכוללים DeleteContentRangeRequest.

דוגמת הקוד הבאה מראה איך אפשר למחוק את הטקסט שבאינדקס 10 ואינדקס 24 בגוף המסמך.

Java

List<Request> requests = new ArrayList<>();
requests.add(new Request().setDeleteContentRange(
        new DeleteContentRangeRequest()
                .setRange(new Range()
                        .setStartIndex(10)
                        .setEndIndex(24)
                        .setTabId(TAB_ID))
    ));

BatchUpdateDocumentRequest body = new BatchUpdateDocumentRequest().setRequests(requests);
BatchUpdateDocumentResponse response = docsService.documents()
        .batchUpdate(DOCUMENT_ID, body).execute();

PHP

$requests = array();
$requests[] = new Google_Service_Docs_Request(array(
    'deleteContentRange' => array(
        'range' => array(
            'startIndex' => 10,
            'endIndex' => 24,
            'tabId' => TAB_ID
        ),
    ),
));

$batchUpdateRequest = new Google_Service_Docs_BatchUpdateDocumentRequest(array(
    'requests' => $requests
));

$response = $service->documents->batchUpdate($documentId, $batchUpdateRequest);

Python

requests = [
    {
        'deleteContentRange': {
            'range': {
                'startIndex': 10,
                'endIndex': 24,
                'tabId': TAB_ID
            }

        }

    },
]
result = service.documents().batchUpdate(
    documentId=DOCUMENT_ID, body={'requests': requests}).execute()

כדי לפשט את העניינים, כותבים אחורה. בדומה להוספות, מחיקה הטקסט משנה את האינדקסים של כל הטקסט 'למטה' בקטע. שוב, כתיבה אחורה יכול לפשט את הטיפול באינדקסים.

הזזת הטקסט

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