问题
I'm trying to figure out how to tell the script to print to a specific sheet named 'Creative' and starting in cell B2. Instead of printing to The code, I'm using the code below. Thanks so much for any help you can offer.
function getFileArray(folderId){
var folder = DriveApp.getFolderById(folderId);
var files = folder.getFiles();
var fileList = [];
//Loop though files and add names and urls to the array
while (files.hasNext()){
var file = files.next();
var fileName = file.getName();
var fileUrl = file.getUrl();
fileList = fileList.concat([[fileName, fileUrl]]);
}
//See returned fileList in a log
//Logger.log( fileList ) //Preview Returned Array
return fileList
}
//Prints any 2D array to a range that starts with the selected cell
function printArrayToSelection(twoDimArr){
var firstCell = SS.getActiveCell();
var lastCell = firstCell.offset(twoDimArr.length - 1, twoDimArr[0].length - 1);
var destinationRange = SS.getActiveSheet().getRange(
firstCell.getA1Notation() + ':' + lastCell.getA1Notation());
destinationRange.setValues(twoDimArr);
}
//Print the actual data
function printFileArray(){
printArrayToSelection(getFileArray('FOLDERIDHERE'));
}
回答1:
To specify the sheet for the destination range, you can use the getSheetByName method and pass "Creative" as an argument.
To get the destination range, I would recommend using the getRange method and passing the row, column, number of rows, and number of columns as arguments. For your case, this would be 2, 2, twoDimArr.length, twoDimArr[0].length
The documentation for this method can be found here: https://developers.google.com/apps-script/reference/spreadsheet/sheet#getRange(Integer,Integer,Integer,Integer)
function printArrayToSelection(twoDimArr){
var firstCell = SS.getActiveCell();
var lastCell = firstCell.offset(twoDimArr.length - 1, twoDimArr[0].length - 1);
var destinationRange = SS.getSheetByName("Creative").getRange(2, 2, twoDimArr.length, twoDimArr[0].length);
destinationRange.setValues(twoDimArr);
}
来源:https://stackoverflow.com/questions/61829578/change-google-apps-script-from-printing-array-from-active-cell-to-specific-sheet