[英]Apps script that shares a google sheet to a specific group of people
我有一個應用程序腳本,每次在主表的 sheet1 的 A 列中創建一個新條目時都會創建一個新的 Google 工作表,並將該新工作表重命名為主表 sheet1 的 A 列中最后輸入的數據。 它還將新創建的工作表的 URL 粘貼到主工作表的 sheet1 的 J 列中。 下面是代碼
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());
}
應用程序腳本代碼由我手動設置的 onEdit 觸發器運行。 每次創建新工作表時,我希望將編輯的最后一行(從 A 到 J)復制到新創建的工作表我還希望共享主工作表和新創建的工作表的每個工作表(作為編輯) 在主表的表 2 的 B 列中包含電子郵件列表。 我感謝您的幫助。
我相信你的目標如下。
getLastRow()
被使用了 2 次。 這個可以用一次。當這些點反映在你的腳本中時,它變成如下。
從a list of emails in Column B of Sheet 2 of the master sheet
,此修改后的腳本從活動電子表格的“Sheet2”的“B”列中檢索電子郵件。 如果您的工作表名稱不是“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.addEditors(emails);
.建議:
這是腳本的一個版本,其中主工作表的最后一行(“A”到“J”列) - 工作表 1 會自動復制到新創建的電子表格的第一行。 有關所提供的更改,請參閱內聯注釋。
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.