Google Apps脚本未检测到当前行

问题描述 投票:0回答:1

我创建了一个与Google电子表格中的自定义菜单项绑定的函数,用于检测当前行并执行一些检查。问题是,每次我运行菜单项>脚本时,它总是检测第1行,而不是当前脚本。这是供参考的代码:

function updateCalendarItems(){


//this function detects the current row and updates the main and follow up calendar events
  //Get the current row
  var ss = SpreadsheetApp.openById('<deleted>');
  var sheet = ss.getSheetByName('db_clientvisit');

  Logger.log('Sheet Name: '+sheet.getName()); //this logs the correct sheet name
//  var range = sheet.getActiveCell();
  var rowNum = sheet.getActiveCell().getRow();
  Logger.log(rowNum); //always logs 1
  var range = sheet.getRange(rowNum, 1, 1, 13).getValues(); //always returns the first row
  Logger.log(range);
  var ui = SpreadsheetApp.getUi();
  ui.alert('Row Num is :'+rowNum+' Column Num is: '+sheet.getActiveCell().getColumn());
  return;
  var range = sheet.getRange(rowNum, 1, 1, 13);
  var row = range.getValues()[0];
  Logger.log(row);
  //open the calendar 
  var calendarID = 'loveyourself.ph_umvj5k6vo45ei0mbh423g97ebg@group.calendar.google.com';
  var calendar = CalendarApp.getCalendarById(calendarID);
  //update the calendar...
}
javascript google-apps-script google-sheets
1个回答
0
投票

使用var ss = SpreadsheetApp.getActive()

使用SpreadsheetApp.openById('<deleted>')时,实际上是在打开电子表格的新会话而不是活动会话。因此,Apps Script无法获得“活动”范围。

类似地,您可能还想尝试使用SpreadsheetApp.getCurrentCell()

SpreadsheetApp.getCurrentCell()
© www.soinside.com 2019 - 2024. All rights reserved.