获取和设置单元格的值不起作用

phi*_*611 3 google-sheets google-apps-script

我无法从电子表格中的单元格获取值。我在文档中找到了 getCell() 方法,但它不起作用。它总是只得到“范围”而不是值。(顺便说一句,它是一个整数。)而且我没有找到 setCell() 方法!oO 值位于单元格 B:1 中,如有任何帮助,谢谢!!1 :)

var API_KEY           = "***",
    PROFILE_ID        = "****";
    GET_PLUS_ONES_URI = "https://www.googleapis.com/plus/v1/people/"+PROFILE_ID+"?fields=plusOneCount&key="+API_KEY;
var currentPlusOnes = 0,
    lastPlusOnes    = 0,
    ss              = SpreadsheetApp.getActive();

function execute() {
  getCurrentPlusOnes();
  getLastPlusOnes();
  if ( currentPlusOnes !== getLastPlusOnes ) {
    setCurrentValue();
  }
}

function getCurrentPlusOnes() {
  currentPlusOnes = JSON.parse( UrlFetchApp.fetch( GET_PLUS_ONES_URI )).plusOneCount;
}

function getLastPlusOnes() {
  lastPlusOnes = ss.getRange("B1").getCell( 1, 1 ); // --> Allways just "Range" instead of the value
}

function setCurrentValue() {
  ss.getDataRange().setValue( currentPlusOnes );
}
Run Code Online (Sandbox Code Playgroud)

JSD*_*ton 5

两者getRange()getCell()返回一个Range对象。您需要打电话getValue()getValues()在范围内获取数据。在这种情况下,由于您的第一个getRange("B1")仅引用单个单元格,因此您不需要getCell(1,1)

lastPlusOnes = ss.getRange("B1").getValue();
Run Code Online (Sandbox Code Playgroud)

https://developers.google.com/apps-script/reference/spreadsheet/range#getValue()

对于设置同样适用。单个单元格中的单个值将是:

Range.setValue( number | string )
Run Code Online (Sandbox Code Playgroud)

或者,如果您有一堆行和列,则首先选择适当大小的范围,然后使用 setValues([][])