我创建了一个脚本,用于将多个选项卡中的信息组合到“主元素”选项卡中。该脚本插入带有“ MS”标记的行,并且效果很好。但是,我也想通过“ MS”为每一行提取Sheetname,Sheet IT和possiby的链接到“源”选项卡。该信息当前未存储在任何源选项卡中。这可以用脚本吗?
axios.patch(url, j, {
headers: {
Authorization: 'Bearer ' + localStorage.getItem('accessToken')
},
onUploadProgress: progressEvent => {
this.basic.status = 'Uploaded: ' + progressEvent.loaded + 'b of ' + progressEvent.total + 'b'
}
}).then(response => (this.handleUploadComplete(response)));
答案 0 :(得分:0)
OP提供的工作代码可以从两张特定的工作表复制行,并在A列中的值为“ MS”。但是,OP还希望:
这些很容易产生,但是需要太多信息才能在评论中解释。
我已经获取了OP的原始代码。我移动和/或编辑了一些行,以使代码效率更高。我还添加了几行代码,以将工作表名称插入Masterelements:H列,将工作表ID插入Masterelements:I列,并在Masterelements:Column J处插入相关工作表的链接。
我认为存在使代码更高效(特别是在循环内)的范围,但这是OP可以考虑的,如果“真实数据”的执行时间不合理。
function so54432164() {
// setup the spreadsheet
var ss = SpreadsheetApp.getActiveSpreadsheet();
// get the spreadsheet URL - required for the hyperlink
var ssurl = ss.getUrl();
//Logger.log("DEBUG: The url for this spreadsheet is "+ssurl);//DEBUG
var destination = ss.getSheetByName('Masterelements');
// CLEAR DESTINATION TAB BEFORE REPOPULATION
// clear the entire destination sheet; this is more efficient that just clearing a nominated range
destination.clearContents();
// IDENTIFY THE first ROW TO CONSOLIDATE ITEMS
var destRow = destination.getLastRow() + 1
//VARIABLE TO CYCLE THROUGH SPECIFIC SHEETS
// note: no comma after the second tab name
var tabs = [
'A-000',
'B-123'
];
//Destination parameters
// note: brought this out of the "for" loop since only need to be declared once.
var numberOfColumns = 7;
var numberOfRows = 1;
var startColumn = 1;
var startRow = 2;
// used for destination.getRange();
// Loop through the sheets
for (var s = 0; s < tabs.length; s++) {
var sheet = ss.getSheetByName(tabs[s]);
// create variables to get the Sheet Name and the sheetID
var sheetname = sheet.getSheetName();
var sheetID = sheet.getSheetId();
//Logger.log("DEBUG: sheet: "+sheetname+", SheetID: "+sheetID);//DEBUG
// get the data: the var 'range' was never used, so we can just call the values direct to 'values'.
var values = sheet.getRange(1, 1, sheet.getLastRow(), sheet.getLastColumn()).getValues();
//LOOP through the rows in the target sheet
for (var i = 0; i < values.length; i++) {
//Logger.log("DEBUG: row = " + i+", and Column A value: " + values[i][0]);//DEBUG
// test for "MS" in Column A
if (values[i][0] == 'MS') {
//Logger.log("DEBUG: Found MS, Count: " + i);//DEBUG
// define the range to copy
var rangeToCopy = sheet.getRange((i + 1), 1, numberOfRows, numberOfColumns);
// Logger.log("DEBUG: the rangetocopy is "+rangeToCopy.getA1Notation()); //DEBUG
// Define the destination range
var destinationRange = destination.getRange(destRow, startColumn, numberOfRows, numberOfColumns);
//Logger.log("DEBUG: the destination range "+destinationRange.getA1Notation());//DEBUG
// copy the source to the target
rangeToCopy.copyTo(destinationRange, SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
// create a variable for the hyperlink formula
var formula = '=HYPERLINK("' + ssurl + '#gid=' + sheetID + '")';
// Logger.log("DEBUG: the formula = "+formula);//DEBUG
// create a variable for the extra data: Sheet name, Sheet ID, and the hyperlink to the sheet.
var extradata = [
[sheetname, sheetID, formula]
];
//define the destination range and set the values
destination.getRange(destRow, numberOfColumns + 1, numberOfRows, 3).setValues(extradata);
// increment the destination row
destRow++;
}
}
}
}