Set Google Sheets Column MetaData - Sheet ID is not being Set - Watch a spreadsheet column for being moved

放肆的年华 提交于 2020-01-30 03:55:15

问题


I'm using the Advanced Drive Service to set the Sheets column metadata. The column ID is being set, and I can retrieve it, but the sheetId property is not getting set.

function setShColumnMetadata() {
  var request,response,ss,sh;

  ss = SpreadsheetApp.getActiveSpreadsheet();
  sh = ss.getSheetByName('Employees');

  request = [ 
    {
      createDeveloperMetadata:{

        developerMetadata:{

          metadataKey:"employeeSh_DeptIdCol",
          metadataValue:JSON.stringify({
            writtenBy:Session.getEffectiveUser().getEmail(),
            createdAt:new Date().getTime()
          }),
          location:{
        //See https://developers.google.com/sheets/api/reference/rest/v4/DimensionRange
            dimensionRange: {
              "sheetId":sh.getSheetId(),
              dimension:"COLUMNS",
              startIndex:9,//column number + 1 is the real column being watched
              endIndex:10
//Even though the end index is one more than the start index its only one column
            }
          },
          visibility:"DOCUMENT"      
        }
      }
    }
  ];

  response = Sheets.Spreadsheets.batchUpdate({requests:request}, ss.getId());
  Logger.log('response: ' + response)

}

Verification Response:

{
"replies":[
  {"createDeveloperMetadata":
    {"developerMetadata":
      {"metadataKey":"employeeSh_DeptIdCol",
       "visibility":"DOCUMENT",
       "metadataValue":"{
         \"writtenBy\":\"example@gmail.com\",
         \"createdAt\":1530996621771
        }",

      "metadataId":1988973086,
      "location":{
        "dimensionRange":{
          "startIndex":9,
          "endIndex":10,
          "dimension":"COLUMNS"
        },
        "locationType":"COLUMN"
      }
    }
  }
}],

"spreadsheetId":"ID Removed"

}

The problem is in the dimensionRange

This is how it's designated:

dimensionRange: {
  //See https://developers.google.com/sheets/api/reference/rest/v4/DimensionRange
  "sheetId":sh.getSheetId(),
  dimension:"COLUMNS",
  startIndex:9,  //column number
  endIndex:10
  //Even though the end index is one more than the

//start index its only one column }

This is what comes back:

"dimensionRange":{
  "startIndex":9,
  "endIndex":10,
  "dimension":"COLUMNS"
},

There is no sheetId

If I use the following code to get the saved metadata:

function getTheSettingsBack() {
  var metaDataClass,ssID,stateFound;

  ssID = SpreadsheetApp.getActiveSpreadsheet().getId();

  metaDataClass = Sheets.Spreadsheets.DeveloperMetadata;

  stateFound = metaDataClass.search({
    dataFilters:[{
      developerMetadataLookup: {
        metadataKey: 'employeeSh_DeptIdCol'
      }}]
  }, ssID);

  Logger.log('stateFound: ' + stateFound)

}

It returns:

{"matchedDeveloperMetadata":
[{"dataFilters":[{"developerMetadataLookup":
{"metadataKey":"employeeSh_DeptIdCol"}}],"developerMetadata":
{"metadataKey":"employeeSh_DeptIdCol","visibility":"DOCUMENT",
"metadataValue":"{\"writtenBy\":\"account name here\",\"createdAt\":1530997558830}",
"metadataId":459573349,
"location":{
  "dimensionRange":{"startIndex":9,"endIndex":10,"dimension":"COLUMNS"},
"locationType":"COLUMN"}}}]}

Still no sheetId

I don't know if I'm doing something wrong, or if there is something wrong with the Advanced Sheets Service.

The Sheets Advanced API is ON, and turned on in my cloud console, and the version is version 4.

来源:https://stackoverflow.com/questions/51226995/set-google-sheets-column-metadata-sheet-id-is-not-being-set-watch-a-spreadsh

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!