简体   繁体   English

从 Chrome 扩展程序运行 Google Apps 脚本

[英]Run a Google Apps Script from Chrome Extension

I'm new to Google Chrome Extension development and I want to run an apps script from the extension when I click the button from extension.我是 Google Chrome 扩展程序开发的新手,当我单击扩展程序中的按钮时,我想从扩展程序中运行应用程序脚本。 But I'm not sure how to do it and I've checked the guide but couldn't catch the idea.但我不知道该怎么做,我已经检查了指南但无法理解这个想法。 Here are my codes,这是我的代码,

script.js脚本.js

$(document).ready(function () {
    $('#btnGet').click(function () {
        $(document).load('https://script.google.com/macros/s/AKfycbwpkBW7qKZBeJ011C7j3le4vV8D0SEHu9709mWtEMzJgrJmHnaR/exec');
    });
});

Apps Script应用脚本

function doGet(e){
  function newEntry() {
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var sheet = ss.getActiveSheet();
    var lr = sheet.getLastRow();
    sheet.getRange(lr+1, 1, 1, 3).setValues([["First Name", "Last Name", "Email"]]);
    Browser.msgBox("New Entry Added!");
  }
}

I've managed to run codes from my script.js but I'm not sure how can I run codes from my apps script.我已经设法从我的script.js运行代码,但我不确定如何从我的应用程序脚本运行代码。 How can I run the codes from my apps script?如何从我的应用程序脚本运行代码?

Short answer简答

Instead of代替

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();

try尝试

var id = 'spreadsheet-id'; //replace with your spreadsheet id
var sheetName = 'sheet name'; //replace with your sheet name.
var ss = SpreadsheetApp.openByID(id);
var sheet = ss.getSheetbyName(sheetName);

Explanation解释

The methods that return spreadsheet active objects can be used when the script is being called from the spreadsheet.当从电子表格调用脚本时,可以使用返回电子表格活动对象的方法。 doGet(e) could interact with a spreadsheet but you should specify which is the one that you want to work on. doGet(e) 可以与电子表格交互,但您应该指定要处理的电子表格。

References参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM