I have a multi-sheet spreadsheet of which most of the sheets are 'work'-type sheets which the user has no need to view and, as such, are hidden. One of these sheets, however, is subject to a copy values only and a copy formulas only script operation. My investigations lead me to believe that, for either of these operations to succeed, the sheet must be unhidden and made the active sheet. However, the script/macro code stalls out. The relevant code is below:
/**
* rngPasteValus - paste values only in a specified range
*
* @param {A1Notation} rngA1 - specified range
*/
function rngPasteValus(rngA1) {
var copyRng;
SprdSht = SpreadsheetApp.getActiveSpreadsheet(); // global SprdSht
copyRng = SprdSht.getRange(rngA1); // identify the range
Sht = copyRng.getSheet(); // identify its host sheet
shtUnhide(Sht.getName()); // unhide the sheet
Sht.setActiveRange(copyRng); // establish the active range
copyRng.copyTo(copyRng,SpreadsheetApp.CopyPasteType.PASTE_VALUES,false); // copy all values eliminating any formulae
shtHide(); //hide the active sheet
}
/**
* rngPasteFormula - paste a relocatable formula into al cells in a specified range
*
* @param {A1Notation} rngA1 - cell range
* @param {A1Notation} fRngA1 - cell host from which to acquire the relocatable formula
*/
function rngPasteFormula(rngA1, fRngA1) {
var copyRng, formRng;
SprdSht = SpreadsheetApp.getActiveSpreadsheet(); // establish addressability
copyRng = SprdSht.getRange(rngA1); // locate the (destination)range to which the formula will be copied
formRng = SprdSht.getRange(fRngA1); // locate the cell range holding the formula
shtUnhide(formRng.getSheet().getSheetName()); //unhide the host sheet
SprdSht.setActiveRange(formRng); // activate the host range
formRng.copyTo(copyRng, SpreadsheetApp.CopyPasteType.PASTE_FORMULA, false); // paste formula only to the destination
shtHide(); // hide host sheet
}
/**
* shtHide - hide the current active sheet
*
*/
function shtHide() {
SpreadsheetApp.getActive().getActiveSheet().hideSheet(); // hide the current active sheet
}
/**
* shtUnhide unhide a named sheet
*
* @param {string} shtName - name of subject sheet
*/
function shtUnhide(shtName) {
var namedSht; // sheet object
namedSht = SpreadsheetApp.getActive().getSheetByName(shtName); // identify the named sheet
namedSht.showSheet(); namedSht.activate(); // show and activate the named sheet
SpreadsheetApp.setActiveSheet(namedSht, false); // establish the sheet as the active sheet
}
setValues
andgetValues
, rather than activating a range and pasting from it – tehhowch