Sheets की बेहतर सेवा

बेहतर शीट सेवा आपको इनका इस्तेमाल करके Sheets API को ऐक्सेस करने की सुविधा देती है Apps Script. Apps Script में पहले से मौजूद Google Sheets API सेवा की तरह, यह एपीआई, स्क्रिप्ट को Google Sheets में डेटा को पढ़ने, उसमें बदलाव करने, उसे फ़ॉर्मैट करने, और प्रज़ेंट करने की अनुमति देता है. ज़्यादातर मामलों में, पहले से मौजूद सेवा का इस्तेमाल करना आसान होता है, लेकिन यह कुछ अतिरिक्त सुविधाएं भी उपलब्ध हैं.

रेफ़रंस

इस सेवा के बारे में विस्तृत जानकारी के लिए, देखें Sheets API के लिए रेफ़रंस दस्तावेज़. Apps Script की सभी बेहतर सेवाओं की तरह, Sheets की बेहतर सेवा भी इन एपीआई की तरह ही सभी ऑब्जेक्ट, तरीके, और पैरामीटर. ज़्यादा जानकारी के लिए, डिलीवरी के तरीके के हस्ताक्षर तय करने का तरीका देखें.

समस्याओं की रिपोर्ट करने और अन्य सहायता पाने के लिए, देखें Sheets से जुड़ी सहायता गाइड.

नमूना कोड

नीचे दिया गया सैंपल कोड, इस एपीआई के वर्शन 4 का इस्तेमाल करता है; फ़िलहाल, यह Sheets API का सिर्फ़ एक ऐसा वर्शन है जो में अपग्रेड कर सकते हैं.

किसी रेंज से वैल्यू पढ़ना

नीचे दिए गए उदाहरण में, बताए गए तरीके से डेटा की वैल्यू को पढ़ने का तरीका बताया गया है डाउनलोड करने की सुविधा देता है. यह किसी एक रेंज को पढ़ना रेसिपी का नमूना.

Advanced/sheets.gs
/**
 * Read a range (A1:D5) of data values. Logs the values.
 * @param {string} spreadsheetId The spreadsheet ID to read from.
 * @see https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/get
 */
function readRange(spreadsheetId = yourspreadsheetId) {
  try {
    const response = Sheets.Spreadsheets.Values.get(spreadsheetId, 'Sheet1!A1:D5');
    if (response.values) {
      console.log(response.values);
      return;
    }
    console.log('Failed to get range of values from spreadsheet');
  } catch (e) {
    // TODO (developer) - Handle exception
    console.log('Failed with error %s', e.message);
  }
}

एक से ज़्यादा रेंज में वैल्यू लिखें

नीचे दिए गए उदाहरण में, अलग-अलग, असंयुक्त डेटा को लिखने का तरीका बताया गया है एक अनुरोध के साथ शीट में रेंज. यह एक से ज़्यादा रेंज में लिखना रेसिपी का नमूना.

Advanced/sheets.gs
/**
 * Write to multiple, disjoint data ranges.
 * @param {string} spreadsheetId The spreadsheet ID to write to.
 * @see https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/batchUpdate
 */
function writeToMultipleRanges(spreadsheetId = yourspreadsheetId) {
  // Specify some values to write to the sheet.
  const columnAValues = [
    ['Item', 'Wheel', 'Door', 'Engine']
  ];
  const rowValues = [
    ['Cost', 'Stocked', 'Ship Date'],
    ['$20.50', '4', '3/1/2016']
  ];

  const request = {
    'valueInputOption': 'USER_ENTERED',
    'data': [
      {
        'range': 'Sheet1!A1:A4',
        'majorDimension': 'COLUMNS',
        'values': columnAValues
      },
      {
        'range': 'Sheet1!B1:D2',
        'majorDimension': 'ROWS',
        'values': rowValues
      }
    ]
  };
  try {
    const response = Sheets.Spreadsheets.Values.batchUpdate(request, spreadsheetId);
    if (response) {
      console.log(response);
      return;
    }
    console.log('response null');
  } catch (e) {
    // TODO (developer) - Handle  exception
    console.log('Failed with error %s', e.message);
  }
}

नई शीट जोड़ें

नीचे दिए गए उदाहरण में, एक नई शीट बनाने का तरीका बताया गया है. टैब और साइज़. यह शीट जोड़ें रेसिपी का सैंपल.

Advanced/sheets.gs
/**
 * Add a new sheet with some properties.
 * @param {string} spreadsheetId The spreadsheet ID.
 * @see https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/batchUpdate
 */
function addSheet(spreadsheetId = yourspreadsheetId) {
  const requests = [{
    'addSheet': {
      'properties': {
        'title': 'Deposits',
        'gridProperties': {
          'rowCount': 20,
          'columnCount': 12
        },
        'tabColor': {
          'red': 1.0,
          'green': 0.3,
          'blue': 0.4
        }
      }
    }
  }];
  try {
    const response =
      Sheets.Spreadsheets.batchUpdate({'requests': requests}, spreadsheetId);
    console.log('Created sheet with ID: ' +
      response.replies[0].addSheet.properties.sheetId);
  } catch (e) {
    // TODO (developer) - Handle exception
    console.log('Failed with error %s', e.message);
  }
}

पिवट टेबल बनाना

नीचे दिए गए उदाहरण में, सोर्स डेटा से पिवट टेबल बनाने का तरीका बताया गया है. यह पिवट टेबल जोड़ें के बराबर है रेसिपी का नमूना.

Advanced/sheets.gs
/**
 * Add a pivot table.
 * @param {string} spreadsheetId The spreadsheet ID to add the pivot table to.
 * @param {string} pivotSourceDataSheetId The sheet ID to get the data from.
 * @param {string} destinationSheetId The sheet ID to add the pivot table to.
 * @see https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/batchUpdate
 */
function addPivotTable(
    spreadsheetId = yourspreadsheetId,
    pivotSourceDataSheetId= yourpivotSourceDataSheetId,
    destinationSheetId= yourdestinationSheetId) {
  const requests = [{
    'updateCells': {
      'rows': {
        'values': [
          {
            'pivotTable': {
              'source': {
                'sheetId': pivotSourceDataSheetId,
                'startRowIndex': 0,
                'startColumnIndex': 0,
                'endRowIndex': 20,
                'endColumnIndex': 7
              },
              'rows': [
                {
                  'sourceColumnOffset': 0,
                  'showTotals': true,
                  'sortOrder': 'ASCENDING',
                  'valueBucket': {
                    'buckets': [
                      {
                        'stringValue': 'West'
                      }
                    ]
                  }
                },
                {
                  'sourceColumnOffset': 1,
                  'showTotals': true,
                  'sortOrder': 'DESCENDING',
                  'valueBucket': {}
                }
              ],
              'columns': [
                {
                  'sourceColumnOffset': 4,
                  'sortOrder': 'ASCENDING',
                  'showTotals': true,
                  'valueBucket': {}
                }
              ],
              'values': [
                {
                  'summarizeFunction': 'SUM',
                  'sourceColumnOffset': 3
                }
              ],
              'valueLayout': 'HORIZONTAL'
            }
          }
        ]
      },
      'start': {
        'sheetId': destinationSheetId,
        'rowIndex': 49,
        'columnIndex': 0
      },
      'fields': 'pivotTable'
    }
  }];
  try {
    const response = Sheets.Spreadsheets.batchUpdate({'requests': requests}, spreadsheetId);
    // The Pivot table will appear anchored to cell A50 of the destination sheet.
  } catch (e) {
    // TODO (developer) - Handle exception
    console.log('Failed with error %s', e.message);
  }
}