Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

google apps script - get and set Value of a Cell do not work

I have trouble with getting a Value from a Cell in my Spreadsheet. I found the getCell()-method in the documentation but it does not work. It always just gets "Range" instead of the Value. (it is a integer btw.) And I don't find a setCell()-method ! o.O The Value is in the cell B:1 Thx for any help!!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 );
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

both getRange() and getCell() return a Range object. You need to get call getValue() or getValues() on a range to get the data out. In this case as your first getRange("B1") is only referencing a single cell, you don't need the getCell(1,1)

lastPlusOnes = ss.getRange("B1").getValue();

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

for setting the same applies. a single value in a single cell would be:

Range.setValue( number | string )

or if you have a a bunch of rows and columns then first select the appropriate sized range and then use setValues([][])


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...