Googlesheets Apps Script附加行和超链接

时间:2018-04-27 15:03:17

标签: javascript google-sheets-api

我有一个代码,可以在电子表格上进行的每次编辑都附加行,其中包含以下详细信息:

Date, Time, Sheet Name, Cell Location, User

代码是:

function onEdit() {
  var ss = SpreadsheetApp.getActiveSpreadsheet(); 
  var sheet = ss.getActiveSheet();
  var timestamp = new Date();
  var sheetName = sheet.getName();


  sheet.appendRow([Utilities.formatDate(timestamp, "GMT", "dd-MMM-yyyy"),
  Utilities.formatDate(timestamp, "GMT", "E"),
  Utilities.formatDate(timestamp, SpreadsheetApp.getActiveSpreadsheet().getSpreadsheetTimeZone(), "HH:mm:ss"),
  sheetName,
  sheet.getActiveCell().getA1Notation(),
  Session.getActiveUser().getEmail()]);

}

我真正想做的是制作小区位置'使用此功能将部分超链接到原始位置:

sheet.getRange(1,1)
.setValue('=hyperlink("https://docs.google.com/spreadsheets/d/'+ss.getId()
+'/edit#gid='
+ss.getActiveSheet().getSheetId()
+'&range='
+sheet.getActiveCell().getA1Notation()+'")');

所以最终的代码是:

function onEdit() {
  var ss = SpreadsheetApp.getActiveSpreadsheet(); 
  var sheet = ss.getActiveSheet();
  var timestamp = new Date();
  var sheetName = sheet.getName();

  sheet.appendRow([Utilities.formatDate(timestamp, "GMT", "dd-MMM-yyyy"),
  Utilities.formatDate(timestamp, "GMT", "E"),
  Utilities.formatDate(timestamp, SpreadsheetApp.getActiveSpreadsheet().getSpreadsheetTimeZone(), "HH:mm:ss"),
  sheetName,

  .setValue('=hyperlink("https://docs.google.com/spreadsheets/d/'+ss.getId()
   +'/edit#gid='
   +ss.getActiveSheet().getSheetId()
   +'&range='
   +sheet.getActiveCell().getA1Notation()+'")'),

  Session.getActiveUser().getEmail()]);

}

但这不起作用。任何建议将不胜感激。

我怀疑是因为我开始使用.setValue()..看起来不错,但我不确定..

由于

1 个答案:

答案 0 :(得分:1)

在您包含的最终代码中,您没有在.setValue()之前包含范围。在这种情况下,您甚至不需要.setValue,因为您要追加的值直接在行中。所以你可以尝试这样的事情:

function onEdit() {
  var ss = SpreadsheetApp.getActiveSpreadsheet(); 
  var sheet = ss.getActiveSheet();
  var timestamp = new Date();
  var sheetName = sheet.getName();

  var link = '=hyperlink("https://docs.google.com/spreadsheets/d/'+ss.getId()
  +'/edit#gid='+ss.getActiveSheet().getSheetId()+'&range='
  +sheet.getActiveCell().getA1Notation()+'")';

  sheet.appendRow([Utilities.formatDate(timestamp, "GMT", "dd-MMM-yyyy"), 
  Utilities.formatDate(timestamp, "GMT", "E"),
  Utilities.formatDate(timestamp, 
  SpreadsheetApp.getActiveSpreadsheet().getSpreadsheetTimeZone(), "HH:mm:ss"),
  sheetName,link,Session.getActiveUser().getEmail()]);
}