将坐标范围转换为A1Notation

时间:2018-04-12 05:57:07

标签: google-apps-script google-sheets

我想将坐标范围转换为A1Notation。当我把"值"到" A1"在电子表格中,我从{"authMode":{},"range":{"columnStart":1,"rowStart":1,"rowEnd":1,"columnEnd":1},"source":{},"user":{"nickname":"$$$","email":"$$$@gmail.com"},"value":"value"}的{​​{1}}获得e。我使用下面的脚本来获得范围的A1Notation。

onEdit(e)

我可以得到" A1"在function onEdit(e){ Logger.log(typeof e.range) // object Logger.log(e.range.getA1Notation()) // A1 } 。但是当我使用下面的脚本时,

e.range.getA1Notation()

function myFunction(){ var e = {"authMode":{},"range":{"columnStart":1,"rowStart":1,"rowEnd":1,"columnEnd":1},"source":{},"user":{"nickname":"$$$","email":"$$$@gmail.com"},"value":"value"}; Logger.log(typeof e.range) // object Logger.log(e.range.getA1Notation()) // Error } 处的返回错误。错误消息为Logger.log(e.range.getA1Notation())TypeError: Cannot find function getA1Notation in object [object Object]都是"对象"。我在这里有两个问题。

  1. 为什么myFunction()的Logger.log(typeof e.range)出错?
  2. 有没有办法通过e.range.getA1Notation(){"columnStart":1,"rowStart":1,"rowEnd":1,"columnEnd":1}转换为A1Notation?
  3. 非常感谢你的时间。我很抱歉我的问题不成熟。

2 个答案:

答案 0 :(得分:1)

  
      
  1. 为什么myFunction()的e.range.getA1Notation()出错?
  2.   

因为对象的range属性不是正确的类。

  
      
  1. 有没有办法通过{"columnStart":1,"rowStart":1,"rowEnd":1,"columnEnd":1}getA1Notation()转换为A1Notation?
  2.   

有很多方法。也许简单的方法是使用对象上设置的坐标获取范围,使用getA1Notation。

示例:

function myFunction(){
  var o = {"columnStart":1,"rowStart":1,"rowEnd":1,"columnEnd":1};
  var sheet = SpreadsheetApp.getActiveSheet();
  //
  var row = o.rowStart;
  var column = o.columnStart;
  var numRows = o.rowEnd - o.rowStart + 1;
  var numColumns = o.columnEnd - o.columnStart + 1;
  var range = sheet.getRange(row, column, numRows, numColumns);
  var A1Notation = range.getA1Notation();
  Logger.log(A1Notation);
}

答案 1 :(得分:0)

这不是你定义范围的方式。查看several methods for getting a range in the documentation.

function myFunction(){
  var e = {
    "authMode": {},
    "range": SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1").getRange(1,1,1,1),
    "source":{},
    "user": {
      "nickname":"$$$",
      "email":"$$$@gmail.com"
    },
    "value":"value"
  };
  Logger.log(typeof e.range) // object
  Logger.log(e.range.getA1Notation()) // A1
}

function convert(e) {
  var e = {
    "authMode":{},
    "range":{
      "columnStart":1,
      "rowStart":1,
      "rowEnd":1,
      "columnEnd":1
    },
    "source":{},
    "user":{
      "nickname":"$$$",
      "email":"$$$@gmail.com"
    },
    "value":"value"
  };
  var row = e.range.rowStart;
  var column = e.range.columnStart;
  var numRows = e.range.rowEnd - row + 1;
  var numColumns = e.range.columnEnd - column + 1;
  var range = SpreadsheetApp.getActive().getSheetByName("Sheet1").getRange(row, column, numRows, numColumns);
  Logger.log(range.getA1Notation()); // A1
}