স্প্রেডশীট আপডেট করুন

এর সেলগুলিতে থাকা মূল ডেটা ছাড়াও, একটি স্প্রেডশীটে আরও অনেক ধরনের ডেটা অন্তর্ভুক্ত থাকে, যেমন:

  • মাত্রা
  • সেল ফরম্যাট এবং সীমানা
  • নামযুক্ত পরিসর
  • সংরক্ষিত পরিসর
  • শর্তসাপেক্ষ বিন্যাস

এগুলো হলো সেইসব বিভিন্ন ধরণের ডেটার মধ্যে কয়েকটি, যা একটি স্প্রেডশীটের চেহারা এবং কার্যকারিতা নিয়ন্ত্রণ করে। spreadsheets.batchUpdate মেথডটি আপনাকে এই স্প্রেডশীটের যেকোনো বিবরণ আপডেট করার সুযোগ দেয়। পরিবর্তনগুলো একটি ব্যাচে একত্রিত করা হয়, যাতে একটি অনুরোধ অসফল হলেও অন্য কোনো (সম্ভাব্য নির্ভরশীল) পরিবর্তন লেখা না হয়।

এই পৃষ্ঠায় spreadsheets.batchUpdate মেথড ব্যবহারের প্রাথমিক বিষয়গুলো বর্ণনা করা হয়েছে। আপনার যদি সেলের মান ডেটা পড়া এবং লেখার প্রয়োজন হয়, তাহলে আপনি "সেলের মান পড়ুন ও লিখুন" অংশে বর্ণিত spreadsheets.values ​​রিসোর্সটিও ব্যবহার করতে পারেন।

অপারেশনের বিভাগগুলি

spreadsheets.batchUpdate দ্বারা সমর্থিত নির্দিষ্ট অপারেশনগুলোকে নিম্নলিখিত প্রধান বিভাগগুলিতে ভাগ করা যেতে পারে:

বিভাগ বর্ণনা
যোগ করুন (এবং অনুলিপি করুন ) নতুন অবজেক্ট যোগ করুন (কখনও কখনও পুরানো অবজেক্টের উপর ভিত্তি করে, যেমন ডুপ্লিকেট অনুরোধের ক্ষেত্রে)।
আপডেট (এবং সেট ) কোনো অবজেক্টের নির্দিষ্ট কিছু প্রোপার্টি আপডেট করা হয়, যেখানে সাধারণত পুরোনো প্রোপার্টিগুলো অপরিবর্তিত থাকে (অন্যদিকে একটি Set রিকোয়েস্ট পূর্ববর্তী ডেটা ওভাররাইট করে দেয়)।
মুছে ফেলুন বস্তুগুলো সরান।

পরবর্তী অনুচ্ছেদে নির্দিষ্ট অপারেশনগুলোর আচরণ বর্ণনা করতে এই বিভাগগুলো ব্যবহৃত হয়।

ব্যাচ আপডেট অপারেশন

spreadsheets.batchUpdate মেথডটি এক বা একাধিক Request অবজেক্ট গ্রহণ করে কাজ করে, যার প্রতিটি সম্পাদন করার জন্য একটি নির্দিষ্ট ধরনের অনুরোধ উল্লেখ করে। বিভিন্ন ধরনের অনুরোধ রয়েছে। নিচে অনুরোধের প্রকারভেদগুলো বিভিন্ন শ্রেণীতে ভাগ করে একটি বিশদ বিবরণ দেওয়া হলো।

বস্তু যোগ করুন / প্রতিলিপি করুন আপডেট / সেট মুছে ফেলুন
স্প্রেডশীট বৈশিষ্ট্য স্প্রেডশিট বৈশিষ্ট্য আপডেট করার অনুরোধ
শীট AddSheetRequest
ডুপ্লিকেট শীট অনুরোধ
UpdateSheetPropertiesRequest DeleteSheetRequest
মাত্রা ( মাত্রার পরিসর সহ) মাত্রা সন্নিবেশ অনুরোধ
মাত্রা সংযোজন অনুরোধ
UpdateDimensionPropertiesRequest
MoveDimensionRequest
AutoResizeDimensionsRequest
DeleteDimensionRequest
সেল (মান, ফরম্যাট, ডেটা যাচাইকরণ ইত্যাদি সহ) RepeatCellRequest
আপডেটসেলসঅনুরোধ
অ্যাপেন্ডসেলসরিকোয়েস্ট
নামযুক্ত রেঞ্জ নামযুক্ত রেঞ্জ অনুরোধ যোগ করুন নামযুক্ত রেঞ্জ আপডেট করার অনুরোধ DeleteNamedRangeRequest
সীমানা সীমানা হালনাগাদের অনুরোধ
ফিল্টার ( ফিল্টার ভিউ এবং বেসিক ফিল্টার সহ) AddFilterViewRequest
ডুপ্লিকেটফিল্টারভিউঅনুরোধ
আপডেটফিল্টারভিউ অনুরোধ
SetBasicFilterRequest
ClearBasicFilterRequest
ডেটা যাচাইকরণ SetDataValidationRequest
শর্তসাপেক্ষ বিন্যাসের নিয়মাবলী AddConditionalFormatRuleRequest শর্তসাপেক্ষ বিন্যাস নিয়ম অনুরোধ আপডেট করুন শর্তসাপেক্ষ ফরম্যাট নিয়ম অনুরোধ মুছুন
সংরক্ষিত রেঞ্জ AddProtectedRangeRequest UpdateProtectedRangeRequest DeleteProtectedRangeRequest
এমবেডেড অবজেক্ট ( চার্ট সহ) চার্ট যোগ করার অনুরোধ UpdateChartSpecRequest
UpdateEmbeddedObjectPositionRequest
DeleteEmbeddedObjectRequest
একীভূত হয় MergeCellsRequest UnmergeCellsRequest

এছাড়াও কিছু অতিরিক্ত অনুরোধ রয়েছে যা ডেটা ম্যানিপুলেট করার জন্য ব্যবহারকারীর কার্যকলাপের অনুকরণ করে:

সীমা

গুগল শিটস-এ সেল এবং সারির সীমাবদ্ধতা সম্পর্কে আরও জানতে, গুগল ড্রাইভে সংরক্ষণযোগ্য ফাইলসমূহ দেখুন।

মাঠের মাস্ক

অনেক "আপডেট" অনুরোধের জন্য ফিল্ড মাস্কের প্রয়োজন হয়। এগুলি হলো কমা দ্বারা বিভক্ত ফিল্ডের একটি তালিকা, যা কোনো অবজেক্টের অন্যান্য ফিল্ড অপরিবর্তিত রেখে শুধুমাত্র নির্দিষ্ট কিছু ফিল্ড আপডেট করার জন্য ব্যবহৃত হয়। * চিহ্নের একটি ফিল্ড মাস্ককে ওয়াইল্ডকার্ডের মতো গণ্য করা হয় এবং এটি একটি মেসেজের প্রতিটি ফিল্ড নির্দিষ্ট করার সংক্ষিপ্ত রূপ (যার অর্থ হলো, আপনি যদি অনুরোধে কোনো ফিল্ডের জন্য মান নির্দিষ্ট না করেন, তবে সেটি তার ডিফল্ট অবস্থায় ফিরে যেতে পারে)। ফিল্ড মাস্ক সম্পর্কে আরও তথ্যের জন্য, "ফিল্ড মাস্কের ব্যবহার" দেখুন।

নিম্নলিখিত নমুনাটি একটি স্প্রেডশীটের শুধুমাত্র শিরোনাম আপডেট করার জন্য UpdateSpreadsheetPropertiesRequest ব্যবহার করে:

অনুরোধ:

POST .../v4/spreadsheets/spreadsheetId:batchUpdate

অনুরোধের মূল অংশ:

{
  "requests": [{
      "updateSpreadsheetProperties": {
          "properties": {"title": "TITLE"},
          "fields": "title"
      }
  }]
}

স্প্রেডশিটটির নতুন শিরোনাম দিয়ে TITLE প্রতিস্থাপন করুন।

প্রতিক্রিয়া

একটি স্প্রেডশীট আপডেট করার সময়, কিছু ধরণের অনুরোধ প্রতিক্রিয়া ফেরত দিতে পারে। এগুলো একটি অ্যারেতে ফেরত আসে, যেখানে প্রতিটি প্রতিক্রিয়া সংশ্লিষ্ট অনুরোধের ইন্ডেক্সেই থাকে। কিছু অনুরোধের কোনো প্রতিক্রিয়া থাকে না এবং সেগুলোর ক্ষেত্রে প্রতিক্রিয়াটি খালি থাকে।

সাধারণত, "add" অনুরোধের প্রতিক্রিয়া হিসেবে যুক্ত করা অবজেক্টের আইডির মতো তথ্য ফেরত আসে। সমর্থিত প্রতিক্রিয়াগুলির তালিকার জন্য, Responses দেখুন।

উদাহরণ

নিম্নলিখিত কোড নমুনাটি এই কাজগুলো সম্পাদন করে:

  1. title ভেরিয়েবল ব্যবহার করে স্প্রেডশীটের শিরোনাম আপডেট করে।
  2. findreplacement ভেরিয়েবল ব্যবহার করে স্প্রেডশিটের সেলের মান খুঁজে বের করে এবং প্রতিস্থাপন করে।

অ্যাপস স্ক্রিপ্ট

sheets/api/spreadsheet_snippets.gs
/**
 * Updates the specified sheet using advanced sheet services
 * @param {string} spreadsheetId id of the spreadsheet to be updated
 * @param {string} title name of the sheet in the spreadsheet to be updated
 * @param {string} find string to be replaced
 * @param {string} replacement the string to replace the old data
 * @returns {*} the updated spreadsheet
 */
Snippets.prototype.batchUpdate = (spreadsheetId, title, find, replacement) => {
  // This code uses the Sheets Advanced Service, but for most use cases
  // the built-in method SpreadsheetApp.getActiveSpreadsheet()
  //     .getRange(range).setValues(values) is more appropriate.

  try {
    // Change the spreadsheet's title.
    const updateSpreadsheetPropertiesRequest =
      Sheets.newUpdateSpreadsheetPropertiesRequest();
    updateSpreadsheetPropertiesRequest.properties =
      Sheets.newSpreadsheetProperties();
    updateSpreadsheetPropertiesRequest.properties.title = title;
    updateSpreadsheetPropertiesRequest.fields = "title";

    // Find and replace text.
    const findReplaceRequest = Sheets.newFindReplaceRequest();
    findReplaceRequest.find = find;
    findReplaceRequest.replacement = replacement;
    findReplaceRequest.allSheets = true;

    const requests = [Sheets.newRequest(), Sheets.newRequest()];
    requests[0].updateSpreadsheetProperties =
      updateSpreadsheetPropertiesRequest;
    requests[1].findReplace = findReplaceRequest;

    const batchUpdateRequest = Sheets.newBatchUpdateSpreadsheetRequest();
    batchUpdateRequest.requests = requests;

    // Add additional requests (operations)
    const result = Sheets.Spreadsheets.batchUpdate(
      batchUpdateRequest,
      spreadsheetId,
    );
    return result;
  } catch (err) {
    // TODO (developer) - Handle exception
    console.log("Failed with error %s", err.message);
  }
};

জাভা

sheets/snippets/src/main/java/BatchUpdate.java
import com.google.api.client.googleapis.json.GoogleJsonError;
import com.google.api.client.googleapis.json.GoogleJsonResponseException;
import com.google.api.client.http.HttpRequestInitializer;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.gson.GsonFactory;
import com.google.api.services.sheets.v4.Sheets;
import com.google.api.services.sheets.v4.SheetsScopes;
import com.google.api.services.sheets.v4.model.BatchUpdateSpreadsheetRequest;
import com.google.api.services.sheets.v4.model.BatchUpdateSpreadsheetResponse;
import com.google.api.services.sheets.v4.model.FindReplaceRequest;
import com.google.api.services.sheets.v4.model.FindReplaceResponse;
import com.google.api.services.sheets.v4.model.Request;
import com.google.api.services.sheets.v4.model.SpreadsheetProperties;
import com.google.api.services.sheets.v4.model.UpdateSpreadsheetPropertiesRequest;
import com.google.auth.http.HttpCredentialsAdapter;
import com.google.auth.oauth2.GoogleCredentials;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/* Class to demonstrate the use of Spreadsheet Batch Update API */
public class BatchUpdate {
  /**
   * Updates spreadsheet's title and cell values.
   *
   * @param spreadsheetId - Id of the spreadsheet.
   * @param title         - New title of the spreadsheet.
   * @param find          - Find cell values
   * @param replacement   - Replaced cell values
   * @return response metadata
   * @throws IOException - if credentials file not found.
   */
  public static BatchUpdateSpreadsheetResponse batchUpdate(String spreadsheetId,
                                                           String title,
                                                           String find,
                                                           String replacement)
      throws IOException {
        /* Load pre-authorized user credentials from the environment.
           TODO(developer) - See https://developers.google.com/identity for
            guides on implementing OAuth2 for your application. */
    GoogleCredentials credentials = GoogleCredentials.getApplicationDefault()
        .createScoped(Collections.singleton(SheetsScopes.SPREADSHEETS));
    HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(
        credentials);

    // Create the sheets API client
    Sheets service = new Sheets.Builder(new NetHttpTransport(),
        GsonFactory.getDefaultInstance(),
        requestInitializer)
        .setApplicationName("Sheets samples")
        .build();

    List<Request> requests = new ArrayList<>();
    BatchUpdateSpreadsheetResponse response = null;
    try {
      // Change the spreadsheet's title.
      requests.add(new Request()
          .setUpdateSpreadsheetProperties(new UpdateSpreadsheetPropertiesRequest()
              .setProperties(new SpreadsheetProperties()
                  .setTitle(title))
              .setFields("title")));
      // Find and replace text.
      requests.add(new Request()
          .setFindReplace(new FindReplaceRequest()
              .setFind(find)
              .setReplacement(replacement)
              .setAllSheets(true)));

      BatchUpdateSpreadsheetRequest body =
          new BatchUpdateSpreadsheetRequest().setRequests(requests);
      response = service.spreadsheets().batchUpdate(spreadsheetId, body).execute();
      FindReplaceResponse findReplaceResponse = response.getReplies().get(1).getFindReplace();

      System.out.printf("%d replacements made.", findReplaceResponse.getOccurrencesChanged());
    } catch (GoogleJsonResponseException e) {
      // TODO(developer) - handle error appropriately
      GoogleJsonError error = e.getDetails();
      if (error.getCode() == 404) {
        System.out.printf("Spreadsheet not found with id '%s'.\n", spreadsheetId);
      } else {
        throw e;
      }
    }
    return response;
  }
}

জাভাস্ক্রিপ্ট

sheets/snippets/sheets_batch_update.js
function batchUpdate(spreadsheetId, title, find, replacement, callback) {
  const requests = [];
  // Change the spreadsheet's title.
  requests.push({
    updateSpreadsheetProperties: {
      properties: {
        title: title,
      },
      fields: 'title',
    },
  });
  // Find and replace text.
  requests.push({
    findReplace: {
      find: find,
      replacement: replacement,
      allSheets: true,
    },
  });
  try {
    // Add additional requests (operations) ...
    const batchUpdateRequest = {requests: requests};
    gapi.client.sheets.spreadsheets.batchUpdate({
      spreadsheetId: spreadsheetId,
      resource: batchUpdateRequest,
    }).then((response) => {
      const findReplaceResponse = response.result.replies[1].findReplace;
      console.log(`${findReplaceResponse.occurrencesChanged} replacements made.`);
      if (callback) callback(response);
    });
  } catch (err) {
    document.getElementById('content').innerText = err.message;
    return;
  }
}

নোড.জেএস

sheets/snippets/sheets_batch_update.js
import {GoogleAuth} from 'google-auth-library';
import {google} from 'googleapis';

/**
 * Performs a batch update on a spreadsheet.
 * Updates the spreadsheet title and finds and replaces a string.
 * @param {string} spreadsheetId The ID of the spreadsheet to update.
 * @param {string} title The new title for the spreadsheet.
 * @param {string} find The string to find.
 * @param {string} replacement The string to replace the found string with.
 * @return {Promise<object>} The response from the batch update.
 */
async function batchUpdate(spreadsheetId, title, find, replacement) {
  // Authenticate with Google and get an authorized client.
  const auth = new GoogleAuth({
    scopes: 'https://www.googleapis.com/auth/spreadsheets',
  });

  // Create a new Sheets API client.
  const service = google.sheets({version: 'v4', auth});

  // Create a list of requests to be executed in the batch update.
  const requests = [];

  // Request to change the spreadsheet's title.
  requests.push({
    updateSpreadsheetProperties: {
      properties: {
        title,
      },
      fields: 'title',
    },
  });

  // Request to find and replace text.
  requests.push({
    findReplace: {
      find,
      replacement,
      allSheets: true,
    },
  });

  // Add more requests here if needed.

  // Create the batch update request.
  const batchUpdateRequest = {requests};

  // Execute the batch update request.
  const response = await service.spreadsheets.batchUpdate({
    spreadsheetId,
    requestBody: batchUpdateRequest,
  });

  // Get the response from the find and replace request and log the number of occurrences.
  const findReplaceResponse = response.data.replies[1].findReplace;
  console.log(`${findReplaceResponse.occurrencesChanged} replacements made.`);
  return response;
}

পিএইচপি

sheets/snippets/src/SpreadsheetBatchUpdate.php
<?php
use Google\Client;
use Google\Service\Drive;
use Google\Service\Sheets\BatchUpdateSpreadsheetRequest;

/**
 * to batch update a spreadsheet
 */
function batchUpdate($spreadsheetId, $title, $find, $replacement)
    {   
        /* Load pre-authorized user credentials from the environment.
           TODO(developer) - See https://developers.google.com/identity for
            guides on implementing OAuth2 for your application. */
        $client = new Google\Client();
        $client->useApplicationDefaultCredentials();
        $client->addScope(Google\Service\Drive::DRIVE);
        $service = new Google_Service_Sheets($client);
        try{
            //execute the request
            $requests = [
                new Google_Service_Sheets_Request([
              'updateSpreadsheetProperties' => [
                  'properties' => [
                      'title' => $title
                    ],
                    'fields' => 'title'
              ]
          ]),
          new Google_Service_Sheets_Request([
              'findReplace' => [
                  'find' => $find,
                  'replacement' => $replacement,
                  'allSheets' => true
                  ]
                  ])
                ];
                $batchUpdateRequest = new Google_Service_Sheets_BatchUpdateSpreadsheetRequest([
                    'requests' => $requests
                ]);
        $response = $service->spreadsheets->batchUpdate($spreadsheetId, $batchUpdateRequest);
        $findReplaceResponse = $response->getReplies()[1]->getFindReplace();
        printf("%s replacements made.\n",
        $findReplaceResponse->getOccurrencesChanged());
        return $response;
    }
    catch(Exception $e) {
        // TODO(developer) - handle error appropriately
        echo 'Message: ' .$e->getMessage();
      }
    }

পাইথন

sheets/snippets/sheets_batch_update.py
import google.auth
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError


def sheets_batch_update(spreadsheet_id, title, find, replacement):
  """
  Update the sheet details in batch, the user has access to.
  Load pre-authorized user credentials from the environment.
  TODO(developer) - See https://developers.google.com/identity
  for guides on implementing OAuth2 for the application.
  """

  creds, _ = google.auth.default()
  # pylint: disable=maybe-no-member

  try:
    service = build("sheets", "v4", credentials=creds)

    requests = []
    # Change the spreadsheet's title.
    requests.append(
        {
            "updateSpreadsheetProperties": {
                "properties": {"title": title},
                "fields": "title",
            }
        }
    )
    # Find and replace text
    requests.append(
        {
            "findReplace": {
                "find": find,
                "replacement": replacement,
                "allSheets": True,
            }
        }
    )
    # Add additional requests (operations) ...

    body = {"requests": requests}
    response = (
        service.spreadsheets()
        .batchUpdate(spreadsheetId=spreadsheet_id, body=body)
        .execute()
    )
    find_replace_response = response.get("replies")[1].get("findReplace")
    print(
        f"{find_replace_response.get('occurrencesChanged')} replacements made."
    )
    return response

  except HttpError as error:
    print(f"An error occurred: {error}")
    return error


if __name__ == "__main__":
  sheets_batch_update("spreadsheet_id", "title", "find", "replacement")

রুবি

sheets/snippets/lib/spreadsheet_snippets.rb
requests = []
# Change the name of sheet ID '0' (the default first sheet on every
# spreadsheet)
requests.push({
                update_sheet_properties: {
                  properties: { sheet_id: 0, title: 'New Sheet Name' },
                  fields:     'title'
                }
              })
# Find and replace text
requests.push({
                find_replace: {
                  find:        find,
                  replacement: replacement,
                  all_sheets:  true
                }
              })
# Add additional requests (operations) ...

body = { requests: requests }
result = service.batch_update_spreadsheet(spreadsheet_id, body, {})
find_replace_response = result.replies[1].find_replace
puts "#{find_replace_response.occurrences_changed} replacements made."