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. It also pastes the URL of the newly created sheets in Column J of sheet1 of the master sheet. 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. 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. I appreciate your help.
CodePudding user response:
I believe your goal is as follows.
- You want to copy the last row (columns "A" to "J") to the 1st tab of the created Spreadsheet.
- You want to share the active Spreadsheet and the created Spreadsheet with the emails retrieved from column "B" of "Sheet2" in the active Spreadsheet.
- You want to run the script by both the installable OnEdit trigger and the manual execution.
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. 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. If your sheet name is not "Sheet2", please modify it.
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. If you don't want to this, please remove
ss.getEditors().forEach(e => ss.removeEditor(e));
andss.addEditors(emails);
.
References:
CodePudding user response:
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. 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());
}