我认为你的目标如下。
- You want to retrieve the URL of each sheet except for "Main" sheet.
- You want to put the URL into cell "A1" of each sheet.
Modification points:
- Unfortunately, Class Sheet has no method of
getUrl
.
- You say
I want to use Googlesheet script to exctract each sheet URL and paste it in range A1 of each sheet
. But, in your scirpt, s.getRange( B3 ).setValue(s.getUrl());
is used.
如果这些要点反映在样本中,则如下。
Sample script:
function getUrl() {
const ignoreSheet = "Main"; // This is from your question.
const ss = SpreadsheetApp.getActiveSpreadsheet();
const baseUrl = ss.getUrl();
const sheets = ss.getSheets();
sheets.forEach(sheet => {
if (sheet.getSheetName() != ignoreSheet) {
sheet.getRange("A1").setValue(`${baseUrl}#gid=${sheet.getSheetId()}`);
}
});
}
- In this case, the URL of each sheet is created by "getUrl() of Class Spreadsheet" and "getSheetId() of Class Sheet".
- When this script is run, the URLs of each sheet except for "Main" sheet are retrieved and each URL is put into cell "A1" of each sheet.
References:
Added:
您的以下二点:
如果我想忽略多份说明,而不是只一份表格,例如“Main”和“CS”的表,那么我会做些什么?
In this case, how about the following sample script?
function getUrl() {
const ignoreSheets = ["Main", "CS"]; // This is from your question.
const ss = SpreadsheetApp.getActiveSpreadsheet();
const baseUrl = ss.getUrl();
const sheets = ss.getSheets();
sheets.forEach(sheet => {
if (!ignoreSheets.includes(sheet.getSheetName())) {
sheet.getRange("A1").setValue(`${baseUrl}#gid=${sheet.getSheetId()}`);
}
});
}