谷歌应用程序脚本doGet

google apps script doGet

本文关键字:doGet 脚本 应用程序 谷歌      更新时间:2023-09-26

我对这段代码有问题。谷歌弃用了几个正在工作的部分。现在,当制作新表并尝试使用旧代码时,我得到错误,并且无法找到在谷歌文档中进行更改的方法。

function doGet(e) {
    //This is not working?
    if (typeof e.parameter.id  == 'undefined'){     
        return no_id(e) // The URL doesn't have an ?id=345 on the end!
    }
    var id = parseInt( e.parameter.id ) // This is the id of the row in the spreadsheet.
    //Script properties is changed and I think it is now: PropertyService.getScriptProperties() // Get the data from the spreadsheet and get the row that matches the id
    var this_spreadsheet_id = ScriptProperties.getProperty('this_spreadsheet_id')
    var ss = SpreadsheetApp.openById(this_spreadsheet_id)
    var sheet = ss.getSheetByName("Sheet1")
    var range = sheet.getDataRange()
    var last_row = range.getLastRow()
    var last_column = range.getLastColumn()
    for(i = 2; i <= last_row ; i++){
        var this_row = sheet.getRange(i,1 , 1, last_column)
        var values = this_row.getValues()[0]
        var row_id = parseInt( values[0] )
        //row id == id is not working either
        if ( row_id == id){        
            var title = values[5]
            var details = values[8]
            var status_txt = values[7]
            Logger.log( "STATUS: " + status )
            var image_url = values[4]
        }
    }
}

任何想法都很棒!

谢谢,

"ReferenceError: "id"未定义。(第23行,文件"Code")"

在定义id之前,if语句中有一个返回值。所以如果它没有被定义,那么if语句就被触发了。将变量定义移到顶部

var id = parseInt( e.parameter.id ) // This is the id of the row in the spreadsheet.
//Script properties is changed and I think it is now: PropertyService.getScriptProperties() // Get the data from the spreadsheet and get the row that matches the id
var this_spreadsheet_id = ScriptProperties.getProperty('this_spreadsheet_id')
var ss = SpreadsheetApp.openById(this_spreadsheet_id)
var sheet = ss.getSheetByName("Sheet1")
var range = sheet.getDataRange()
var last_row = range.getLastRow()
var last_column = range.getLastColumn()
if (typeof e.parameter.id  == 'undefined'){     
    return no_id(e) // The URL doesn't have an ?id=345 on the end!
}