[英]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.我相信你的目标如下。
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.当这些点反映在你的脚本中时,它变成如下。
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);
}
ss.getEditors().forEach(e => ss.removeEditor(e));
如果您不想这样做,请删除ss.getEditors().forEach(e => ss.removeEditor(e));
and ss.addEditors(emails);
和ss.addEditors(emails);
. .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.