![](/img/trans.png)
[英]Sorting raw data from one sheet into separate tabs on a new sheet in Google App Script
[英]Add a new sheet from template into a raw google script and organize into a folder
我有這個電子表格,我想在每個月的目錄中復制模板,並在每個相應的單元格中添加鏈接,添加相應的數據
現在我添加新工作表,但我想按每個月的目錄組織。
這是可能的?
謝謝
https://docs.google.com/spreadsheets/d/1abdggD73Zb0XmRoFaMx0ssULjtLLTHhFQV3ikbaEK_I/edit?usp=sharing
function newSheet() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var templateSheet = ss.getSheetByName("Plantilla")
var sheet1 = ss.getSheetByName("Sheet1")
var getNames = sheet1.getRange("G2:G").getValues().filter(String).toString().split(",");
for (var i = 0; i < getNames.length; i++) {
var copy = ss.getSheetByName(getNames[i]);
if (copy) {
Logger.log("Sheet already exists");
} else {
templateSheet.copyTo(ss).setName(getNames[i]);
ss.setActiveSheet(ss.getSheetByName(getNames[i]));
ss.moveActiveSheet(ss.getNumSheets());
}
}
}
// function to create the index
function createIndex() {
// Get all the different sheet IDs
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheets = ss.getSheets();
var namesArray = sheetNamesIds(sheets);
var indexSheetNames = namesArray[0];
var indexSheetIds = namesArray[1];
// check if sheet called sheet called already exists
// if no index sheet exists, create one
if (ss.getSheetByName('index') == null) {
var indexSheet = ss.insertSheet('Index',0);
}
// if sheet called index does exist, prompt user for a different name or option to cancel
else {
var indexNewName = Browser.inputBox('The name Index is already being used, please choose a different name:', 'Please choose another name', Browser.Buttons.OK_CANCEL);
if (indexNewName != 'cancel') {
var indexSheet = ss.insertSheet(indexNewName,0);
}
else {
Browser.msgBox('No index sheet created');
}
}
// add sheet title, sheet names and hyperlink formulas
if (indexSheet) {
printIndex(indexSheet,indexSheetNames,indexSheetIds);
}
}
// function to update the index, assumes index is the first sheet in the workbook
function updateIndex() {
// Get all the different sheet IDs
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheets = ss.getSheets();
var indexSheet = sheets[0];
var namesArray = sheetNamesIds(sheets);
var indexSheetNames = namesArray[0];
var indexSheetIds = namesArray[1];
printIndex(indexSheet,indexSheetNames,indexSheetIds);
}
// function to print out the index
function printIndex(sheet,names,formulas) {
sheet.clearContents();
sheet.getRange(1,1).setValue('Workbook Index').setFontWeight('bold');
sheet.getRange(3,1,names.length,1).setValues(names);
sheet.getRange(3,2,formulas.length,1).setFormulas(formulas);
}
// function to create array of sheet names and sheet ids
function sheetNamesIds(sheets) {
var indexSheetNames = [];
var indexSheetIds = [];
// create array of sheet names and sheet gids
sheets.forEach(function(sheet){
indexSheetNames.push([sheet.getSheetName()]);
indexSheetIds.push(['=hyperlink("#gid='
+ sheet.getSheetId()
+ '";"'
+ sheet.getSheetName()
+ '")']);
});
return [indexSheetNames, indexSheetIds];
}
通過在 Apps Script和Spreadsheet 服務中使用Drive 服務,您可以輕松實現您的意圖。 下面是一段自我解釋的注釋代碼,它實現了您的目標。
注意:我只舉了一個例子,說明第一個月 (ENERO) 的情況如何,為了實現其余部分,它只是復制我在第一個月所做的事情。
function myFunction() { // Get the sheet where we have all the dates var sheet = SpreadsheetApp.getActive().getSheetByName('Sheet1'); // Get the month name. Repeat this for the other months you might want to include // For example if February is on A10 just get the range from A10 var Month1 = sheet.getRange('A1').getValue(); // Create a parent folder in the root of your drive where you will be storing everything for this project var parentFolder = DriveApp.createFolder('Months'); // Create a folder for each month using the name we got before. Repeat for the rest of the months. var Month1Folder = DriveApp.getFolderById(parentFolder.getId()).createFolder(Month1); // Get the dates of each month. Repeat for the rest of the months. // For example, if February dates go from A11 to A21 get the range A11:A21 // Also, getValues returns an array of arrays. Therefore I have flatten it to make a simple 1D array var Month1Days = sheet.getRange('A2:A4').getValues().flat(); // For every element of our Month 1 date names array // Repeat this block for the rest of the months substituying the array and column (to not overwrite) accordingly for(i=0;i<Month1Days.length;i++){ // Create a new spreadsheet with the name of the dates of that month and get its id var idFile = SpreadsheetApp.create(Month1Days[i]).getId(); // Get the file object of the new Spreadsheet var file = DriveApp.getFileById(idFile); // Add this file to our month folder Month1Folder.addFile(file); // Remove the duplicate that is still in the root folder DriveApp.getRootFolder().removeFile(file); // Paste a link to this new spreadsheet in our original sheet in the third column sheet.getRange(i+1, 3).setValue(file.getUrl()); } }
我希望這對你有幫助。 如果您需要其他任何東西或者您不理解某些東西,請告訴我。 :)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.