getA1Notation脚本始终返回A1作为活动单元格或选择

时间:2018-03-30 11:36:48

标签: google-apps-script google-sheets

------------------------更新05/04/18 -------------- -------------------------------------------------- ------------------ 背景: 我正在开发Google Spreadsheets的扩展程序,但是为了构建我的自定义UI,我不会使用Html服务或UI服务。我使用传统的纯HTML / JS。所以,我在边栏上渲染我的UI ------------------------结束更新05/04/18 ---------------- -------------------------------------------------- ---------

这就是我在脚本中从电子表格中选择有效范围的方法:

function GetSelectionSpreadSheet()
{
  var app = SpreadsheetApp ;
  var classeur = app.getActiveSpreadsheet();
  var feuille = classeur.getActiveSheet();
  var selectedRng = feuille.getActiveRange();
  var val = selectedRng.getA1Notation();
  Logger.log(val);
  return val;  
}

这就是我如何称呼这个剧本:

  window.gapi.load('client:auth2', () => {
    window.gapi.client.load('script', 'v1', () => {
      const client_id = 'xxx';
      const scope = [
        'https://www.googleapis.com/auth/documents',
        'https://www.googleapis.com/auth/forms',
        'https://www.googleapis.com/auth/script.container.ui',
        /** View and manage your spreadsheets in Google Drive */
        'https://www.googleapis.com/auth/spreadsheets'
      ];
      const immediate = false;
      window.gapi.auth.authorize({ client_id, scope, immediate }, (authResult) => {
        if (authResult && !authResult.error) {
          /** handle succesfull authorization */
          this.run();
        } else {
          /** handle authorization error */
        }
      });
    });
  });
});


 async run() {
    await window.gapi.client.script.scripts.run({
      scriptId: '1u59k_RlcvGfRGVheuS7xIWOHY-YDs8IKdY3VK6KQd_Nx7NuBlyqAnNEN',
      resource: {
        function: 'GetSelectionSpreadSheet',
        devMode: true
      }
    }).then((resp) => {
      const result = resp.result;
      if (result.error && result.error.status) {
        console.log('Error calling API:' + JSON.stringify(result, null, 2));
      } else if (result.error) {
        const error = result.error.details[0];
        console.log('Script error message: ' + error.errorMessage);

        if (error.scriptStackTraceElements) {
          console.log('Script error stacktrace:');
        }
      } else {
        const folderSet = result.response.result;
        console.log(folderSet);
      }
    });
  }

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:3)

这是因为当电子表格刚刚被链接而不是被打开时没有活动选择。