简体   繁体   English

将 Google 表格共享给特定人群的应用程序脚本

[英]Apps script that shares a google sheet to a specific group of people

I have an apps script that creates a new Google worksheet each time a new entry is made in Column A of sheet1 of a Master Sheet and renames that new worksheet to the last entered data in column A of sheet1 of the Master Sheet.我有一个应用程序脚本,每次在主表的 sheet1 的 A 列中创建一个新条目时都会创建一个新的 Google 工作表,并将该新工作表重命名为主表 sheet1 的 A 列中最后输入的数据。 It also pastes the URL of the newly created sheets in Column J of sheet1 of the master sheet.它还将新创建的工作表的 URL 粘贴到主工作表的 sheet1 的 J 列中。 Below is the code下面是代码

function myFunction() {
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getActiveSheet();
  const nss = SpreadsheetApp.create(sh.getRange(sh.getLastRow(), 1).getDisplayValue())
  sh.getRange(sh.getLastRow(), 10).setValue(nss.getUrl());
}

The apps script code is run by an onEdit trigger I set manually.应用程序脚本代码由我手动设置的 onEdit 触发器运行。 Each time a new worksheet is created, I want the last row that was edited to be copied (from A to J) to the newly created worksheet I also want every worksheet both the master sheet and the newly created sheet to be shared (as editors) with a list of emails in Column B of Sheet 2 of the master sheet.每次创建新工作表时,我希望将编辑的最后一行(从 A 到 J)复制到新创建的工作表我还希望共享主工作表和新创建的工作表的每个工作表(作为编辑) 在主表的表 2 的 B 列中包含电子邮件列表。 I appreciate your help.我感谢您的帮助。

I believe your goal is as follows.我相信你的目标如下。

  1. You want to copy the last row (columns "A" to "J") to the 1st tab of the created Spreadsheet.您想将最后一行(列“A”到“J”)复制到创建的电子表格的第一个选项卡。
  2. You want to share the active Spreadsheet and the created Spreadsheet with the emails retrieved from column "B" of "Sheet2" in the active Spreadsheet.您希望与从活动电子表格中“Sheet2”的“B”列检索到的电子邮件共享活动电子表格和创建的电子表格。
  3. You want to run the script by both the installable OnEdit trigger and the manual execution.您希望通过可安装的 OnEdit 触发器和手动执行来运行脚本。

Modification points:修改点:

  • In your script,在你的脚本中,
    • Script for copying the edited row is not included.不包括用于复制已编辑行的脚本。
    • Script for sharing the Spreadsheet is not included.不包括用于共享电子表格的脚本。
    • getLastRow() is used 2 times. getLastRow()被使用了 2 次。 This can be used one time.这个可以用一次。

When these points are reflected in your script, it becomes as follows.当这些点反映在你的脚本中时,它变成如下。

Modified script:修改后的脚本:

From a list of emails in Column B of Sheet 2 of the master sheet , this modified script retrieves the emails from column "B" of "Sheet2" of the active Spreadsheet.a list of emails in Column B of Sheet 2 of the master sheet ,此修改后的脚本从活动电子表格的“Sheet2”的“B”列中检索电子邮件。 If your sheet name is not "Sheet2", please modify it.如果您的工作表名称不是“Sheet2”,请修改它。

function myFunction() {
  // Creating new Spreadsheet.
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getActiveSheet();
  const lastRow = sh.getLastRow();
  const values = sh.getRange(`A${lastRow}:J${lastRow}`).getValues()[0];
  const nss = SpreadsheetApp.create(values[0]);
  const url = nss.getUrl();
  sh.getRange(lastRow, 10).setValue(url);
  values[9] = url;
  nss.appendRow(values);

  // Sharing Spreadsheets.
  const sheet2 = ss.getSheetByName("Sheet2");
  const emails = [...new Set(sheet2.getRange("B1:B" + sheet2.getLastRow()).getValues().reduce((ar, [b]) => {
    if (b && b.includes("@")) ar.push(b);
    return ar;
  }, []))];
  ss.getEditors().forEach(e => ss.removeEditor(e));
  ss.addEditors(emails);
  nss.addEditors(emails);
}
  • When this script is run, I think that the above your expected flow is run.运行此脚本时,我认为上面的预期流程已运行。
  • In this modified script, the active Spreadsheet is sharing with the values from the column "B" of "Sheet2" by updating the users.在此修改后的脚本中,活动电子表格通过更新用户与“Sheet2”的“B”列中的值共享。 If you don't want to this, please remove ss.getEditors().forEach(e => ss.removeEditor(e));如果您不想这样做,请删除ss.getEditors().forEach(e => ss.removeEditor(e)); and ss.addEditors(emails);ss.addEditors(emails); . .

References:参考:

SUGGESTION:建议:

This is a version of the script wherein the last row (Columns "A" to "J") of your Master Sheet - Sheet 1 is automatically copied over the first row of the newly created spreadsheet.这是脚本的一个版本,其中主工作表的最后一行(“A”到“J”列) - 工作表 1 会自动复制到新创建的电子表格的第一行。 See inline comments for the changes provided.有关所提供的更改,请参阅内联注释。

function createNewSheet() {
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getSheetByName('Sheet1');
  const nss = SpreadsheetApp.create(sh.getRange(sh.getLastRow(), 1).getDisplayValue());
  
  //Opens the newly created spreadsheet
  const ds = SpreadsheetApp.openById(nss.getUrl().substring(39, 83)).getActiveSheet(); 

  //Gets the value from the master spreadsheet's last row
  const dsval = sh.getRange(sh.getLastRow(), 1, 1, 10).getValues();

  //Sets the value to the first row of the newly created spreadsheet
  ds.getRange('A1:J1').setValues(dsval);

  sh.getRange(sh.getLastRow(), 10).setValue(nss.getUrl());
}

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

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