我尝试使用Google App Script编写简单的Content Service API。我的doGet()只需要一个参数' row'并从col 2返回该行中的值。这是:
function doGet(req){
try{
reqRow = req.queryString.split('=')[1];
rowNum = parseInt(reqRow);
}
catch(err){
throw "Error: "+ err + " -- Here's what we got: " + req + "Did you include a row parameter?";
}
resultValue = getFromSheet(rowNum);
if(resultValue != ""){
result = {
"Result":"Success",
"Row": reqRow,
"Value" : resultValue
}
}
else{
result = {
"Result":"Failure",
"Row": reqRow,
"Value" : "No value found at row"
}
}
return ContentService.createTextOutput(JSON.stringify(result)).setMimeType(ContentService.MimeType.JSON);
}
这是我们简单的getFromSheet函数:
function getFromSheet(row){
if(typeof row != "number"){
row = Number(row);
Logger.log("converting");
}
sheetid = 111111111;
ss = SpreadsheetApp.openById(sheetid);
sheet = ss.getSheets()[0];
return ss.getRange(row, 2).getValue();
}
当我发布内容时,通过类似于
的网址"https://script.google.com/macros/s/111111111/exec?row=3"
我收到以下错误:
Cannot find method getRange(number,number).
注意:为什么我在doGet()方法中没有使用req.parameters.row?因为我收到以下错误:
Cannot find method getRange((class),number).
不是getRange(数字,数字)是调用此方法的主要方式吗?
以下是工作表类的参考指南: https://developers.google.com/apps-script/reference/spreadsheet/sheet
谢谢大家!
答案 0 :(得分:4)
将变量名称更改为rowRequest而不是row。
function getFromSheet(rowRequest){
if(typeof rowRequest != "number"){
rowRequest = Number(rowRequest);
Logger.log("converting");
}
ss = SpreadsheetApp.openById('12r0gHSgvBg8s_RJIuWJl-3aZCAGOpW0PfGb0SVGNu4Q');
sheet = ss.getSheets()[0];
result = sheet.getRange(rowRequest, 2).getValue();
return result;
}