简体   繁体   English

如何使用可安装的触发器通过从单元格调用它来使 SpreadsheetApp.openById(id) 工作

[英]How to use an installable trigger to get SpreadsheetApp.openById(id) to work, by calling it from a cell

I have a sheet where users download the latest version (make a copy of) from a blank master.我有一张表,用户可以从空白母版下载最新版本(制作副本)。 I want them to be able to import their 'logs' (2 sheets with ranges) from whatever version they last had by copying the spreadsheet id from their original sheet and pasting it into a box and pressing a button that executes the installable trigger script.我希望他们能够通过从原始工作表中复制电子表格 ID 并将其粘贴到框中并按下执行可安装触发脚本的按钮,从他们上次使用的任何版本导入他们的“日志”(2 张带范围的工作表)。

In this instance, both sheets would be in the users drive.在这种情况下,两个工作表都在用户驱动器中。 I know there are some limitations with openByID.我知道 openByID 有一些限制。

I found a few things on here, but I'm unsure of how to call or parse the pasted id from the cell, as the example below (which I found here) seems wrong since it just defines a variable twice.我在这里发现了一些东西,但我不确定如何从单元格中调用或解析粘贴的 id,因为下面的示例(我在此处找到)似乎是错误的,因为它只定义了一个变量两次。

function setTrigger() {
  var ss = SpreadsheetApp.getActive();
  ScriptApp.newTrigger("importlogs").forSpreadsheet(ss).onEdit().create();
}

//variables
var id = "123456789abcdefg";
var sheet = "LOG WIZARD";
var cells = "M11:AB13";
var range = SpreadsheetApp.openById(id).getSheetByName(sheet).getRange(cells);
var id = range.getSheet().getParent().getId();

//custom function to import logs
function importlogs() {

//Source sheet from which to import from
var is = SpreadsheetApp.openById(id)
var sheet1i = is.getSheetByName("BUDGET LOG");
var sheet2i = is.getSheetByName("LOG");

//Current sheet from which to export to
var xs = SpreadsheetApp.getActiveSpreadsheet();
var sheet1x = xs.getSheetByName("BUDGET LOG");
var sheet2x = xs.getSheetByName("LOG");

//Copy and paste contents of import Budget Log sheet to export Budget Log sheet
sheet1i.getRange("A3:AO").copyTo(sheet1x.getRange(sheet1x.getLastRow()+1,1,1,7), {contentsOnly:true});

//Copy and paste contents of import Log sheet to export Log sheet
sheet2i.getRange("A3:O").copyTo(sheet2x.getRange(sheet2x.getLastRow()+1,1,1,7), {contentsOnly:true});

}

When the trigger fires, you can examine the event object e to find which cell was edited.当触发器触发时,您可以检查事件 object e以查找编辑了哪个单元格。 If it is the cell you are interested in, you can call the import function with the value that was entered, like this:如果它是您感兴趣的单元格,您可以使用输入的值调用导入 function,如下所示:

/**
* Installable trigger that runs each time the user hand edits the spreadsheet.
*
* @param {Object} e The installable "on edit" event object.
*/
function runImportLogs(e) {
  if (!e) {
    throw new Error(
      'Please do not run the runImportLogs(e) function in the script editor window. '
      + 'It runs automatically when you hand edit the spreadsheet.'
    );
  }
  if (!e.value
    || e.range.getA1Notation() !== 'M11'
    || e.range.getSheet().getName() !== 'LOG WIZARD') {
    return;
  }
  importLogs_(e.value);
}

To use the passed value in the import function, declare it as an argument, like this:要在导入 function 中使用传递的值,请将其声明为参数,如下所示:

/**
* Imports logs.
*
* @param {Object} sourceSsId The ID of the spreadsheet where to read data from.
*/
function importLogs_(sourceSsId) {
  try {
    const sheetNames = ['BUDGET LOG', 'LOG',];
    const sourceSs = SpreadsheetApp.openById(sourceSsId);
    const sourceSheets = sheetNames.map(sheetName => sourceSs.getSheetByName(sheetName));
    const targetSs = SpreadsheetApp.getActive();
    const targetSheets = sheetNames.map(sheetName => targetSs.getSheetByName(sheetName));
    sourceSheets.forEach((sheet, index) => {
      const data = sheet.getRange('A3:AO').getValues();
      appendRows_(targetSheets[index], data);
    });
  } catch (error) {
    SpreadsheetApp.getActive().toast(error.message);
    throw error;
  }
}

For this to work, you will need to paste the appendRows_() utility function in the script project.为此,您需要将appendRows_()实用程序 function 粘贴到脚本项目中。

Finally, when you are setting up the trigger, it usually makes sense to first delete any duplicate triggers that may still be hanging around:最后,当您设置触发器时,首先删除可能仍然存在的任何重复触发器通常是有意义的:

function setTrigger() {
  const ss = SpreadsheetApp.getActive();
  const triggers = ScriptApp.getUserTriggers(ss);
  triggers.forEach(trigger => {
    if (trigger.getEventType() === ScriptApp.EventType.ON_EDIT) {
      ScriptApp.deleteTrigger(trigger);
    }
  });
  ScriptApp.newTrigger('runImportLogs').forSpreadsheet(ss).onEdit().create();
}

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

相关问题 Google Apps 脚本:onEdit 在 SpreadsheetApp.openById() 上中断 - 需要在 Installable onEdit() 触发器上输入 - Google Apps Script: onEdit breaks on SpreadsheetApp.openById() - Need input on Installable onEdit() Trigger 使用变量插入SpreadsheetApp.openById - Use a variable to insert into a SpreadsheetApp.openById 使用谷歌表格脚本 function SpreadsheetApp.openById 触发 onEdit 时出现权限错误 - 如何解决 - privilege error on trigger onEdit with google sheets script function SpreadsheetApp.openById - How to solve SpreadsheetApp.openById(id)v2电子表格上的通用错误 - Generic Error on SpreadsheetApp.openById(id) v2 Spreadsheet 调用 SpreadsheetApp.OpenById() 的权限 - Permission to Call SpreadsheetApp.OpenById() 如何在SpreadSheetApp.openById()中使用getRange的四参数形式? - How can I use the four-argument form of getRange with SpreadSheetApp.openById()? 使用appscript的SpreadsheetApp.openById错误 - SpreadsheetApp.openById error using appscript SpreadsheetApp.openByID的突然权限问题 - Sudden permissions problem with SpreadsheetApp.openByID SpreadsheetApp.openById没有通过Google电子表格 - SpreadsheetApp.openById not going through in Google Spreadsheet SpreadsheetApp.openById 上的脚本失败 - 需要权限 - Script fails on SpreadsheetApp.openById - Requires permission
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM