触发器无法在Apps脚本中运行

时间:2014-09-17 07:45:56

标签: google-apps-script

我想每小时运行一次代码,但它无法正常工作。我的意思是说,当手动运行应用程序脚本时,它很好,但我已经把这个代码的触发器按时间驱动了每小时。

但是这个应用脚本并没有每小时更新一次我的电子表格。 请为我提供一些解决方案。

function gControlMonitoring()
{
    var sheet= SpreadsheetApp.getActiveSpreadsheet();
    // var ss=sheet.getActiveSheet();
    // var sheet= SpreadsheetApp.openById('19Fk3pxtfCqP_6Lj-bZvI7UO_ejqA-c1OQBNaBKLa2Tk');
    // SpreadsheetApp.setActiveSpreadsheet(sheet);
    var ss=sheet.getActiveSheet();
    //  var ss =SpreadsheetApp.getActiveSpreadsheet();
    var resp = UrlFetchApp.fetch("https://mail.google.com/a/advanceforensictools.com");
    Logger.log(resp);
    if(resp.getContentText().indexOf('ssomanagerservlet')>-1)
    {
        var d=new Date();
        var dateformat=Utilities.formatDate(d, "Asia/Kolkata", "MM/dd/yyyy");
        Logger.log("dateformat=" + dateformat); //Taking the Current date 
        // var cell = sheet.getRange(2,1); //updating the spreadsheet with current date 
        // cell.setValue(" " +dateformat);
        var timeFormat=Utilities.formatDate(d, "Asia/Kolkata", "HH");//Taking current Time
        Logger.log("dateTime=" + timeFormat);
        var data_column = ss.getRange(1,1,1,ss.getLastColumn()).getValues();//putting all the values of row one to data_column instance
        Logger.log("record" +data_column);

        if(timeFormat==0)   
        {
            var newrow=updatingspread();//Add New row if a new day has start
            var cell = sheet.getRange(2,1); //updating the spreadsheet with current date 
            cell.setValue(" " +dateformat);  
        }

        for(var i=0;i<25;i++)
        {
            var datacol=data_column[0];
            Logger.log(datacol[i]);
            if(datacol[i]==timeFormat)
            {
                ss.getRange(2, i+1).setBackground('green').setValue(1);
                // if(i==23)
                // {
                //     sheet.getRange(2, i+1).setBackground('green').setValue(1);
                //     var newrow=updatingspread();               
                // }
            }
        }      
    } 
    else 
    {
        for(var i=0;i<25;i++)
        {
            var datacol=data_column[0];
            Logger.log(datacol[i]);
            if(datacol[i]==timeFormat)
            {
                sheet.getRange(2, i+1).setBackground('red').setValue(0);
                if(i==23)
                {
                    ss.getRange(2, i+1).setBackground('red').setValue(0);
                    var newrow=updatingspread();
                }
                GmailApp.sendEmail('abc@gmail.com', 'hello', 'Hello Team');
                Logger.log(response.getResponseCode());
                Logger.log(response.getContentText());
            }
        }
    }
}


function updatingspread()
{
    var newSheet = SpreadsheetApp.getActiveSpreadsheet();
    var sheet = newSheet.insertRowBefore(2);
    var data_column1 = sheet.getRange(2,1,1,sheet.getLastColumn()).getBackgrounds();
    Logger.log(data_column1);
    for(i=0;i<25;i++)
    {
        var dataval=data_column1[0];
        Logger.log(dataval[i]);
        if(dataval[i]!='white'&& dataval[i]!='#ffffff')
        {
            var newup=sheet.getRange(2,i+1).setBackgroundRGB(255,255,255);
        }
    }
    var range = sheet.getRange('A7');
    sheet.hideRow(range);
}

1 个答案:

答案 0 :(得分:1)

当您在定时器触发器上运行访问电子表格的功能时,您必须使用openById(&#34; ss id long string&#34;)而不是getActiveSpreadsheet和getSheetByName(&#34;工作表名称&#34;)代替getActiveSheet因为在此上下文中没有活动的电子表格。