I have used this script in the past to edit a cell or range of cells in a set of spreadsheets in a specific folder (using a source file), but when I run the script it does not work. In fact, instead of copying the range from the source sheet it clears out existing data in the destination sheets.
Here is script I have been using:
var files = DriveApp.getFolderById("1NNHFL6fMFz-LwJ4tZrvTN92foXhf7VS8").getFiles()
while (files.hasNext()) {
var file = files.next();
var shoot = SpreadsheetApp.openById(file.getId());
var sourcesheet = SpreadsheetApp.getActive().getSheetByName("Sheet1");
var sourcerange = sourcesheet.getRange('A:A');
var sourcevalues = sourcerange.getFormulas();
var destsheet = shoot.getSheetByName('Sheet1');
var destrange = destsheet.getRange('A:A');
destrange.setValues(sourcevalues);
}
}
Here is a link to the folder I have been testing this in.
Your script works fine if you change setValues() to setFormulas (if you need formulas):
function myFunction() {
var files = DriveApp.getFolderById("1NNHFL6fMFz-LwJ4tZrvTN92foXhf7VS8").getFiles()
while (files.hasNext()) {
var file = files.next();
var shoot = SpreadsheetApp.openById(file.getId());
var sourcesheet = SpreadsheetApp.getActive().getSheetByName("Sheet1");
var sourcerange = sourcesheet.getRange('A:A');
var sourcevalues = sourcerange.getFormulas();
var destsheet = shoot.getSheetByName('Sheet1');
var destrange = destsheet.getRange('A:A');
destrange.setFormulas(sourcevalues); // <------------- HERE
}
}
Try this:
function myfunc() {
var files = DriveApp.getFolderById("1NNHFL6fMFz-LwJ4tZrvTN92foXhf7VS8").getFiles()
const ss=SpreadsheetApp.getActive();
var sourcesheet = ss.getSheetByName("Sheet1");
var sourcerange = sourcesheet.getRange(1,1,sourcesheet.getLastRow(),1);
var sourcevalues = sourcerange.getFormulas();
while (files.hasNext()) {
var file = files.next();
var shoot = SpreadsheetApp.openById(file.getId());
var destsheet = shoot.getSheetByName('Sheet1');
var destrange = destsheet.getRange(1,1,sourcesheet.getLastRow(),1);
destrange.setFormulas(sourcevalues);
}
}
Related
i have to copy my sheet from file1 (16aWONG-TyHYxYFYqfDI7Pw2PVuNvDQ0cnFoIv_SDA4U) into file2 (1zIeqvQuC7RlXy9SB--SaNaEXKT399NvffM3F3nfAXGs)
i have this script, but it didnt work, i don't know where to put fromfile
function getdata() {
var files = DriveApp.getFileById("1zIeqvQuC7RlXy9SB--SaNaEXKT399NvffM3F3nfAXGs").getFiles()
while (files.hasNext()) {
var file = files.next();
var shoot = SpreadsheetApp.openById(file.getId());
var sourcesheet = SpreadsheetApp.getActive().getSheetByName("BahanMix");
var sourcerange = sourcesheet.getRange('A:D');
var sourcevalues = sourcerange.getValues();
var destsheet = shoot.getSheetByName('Sheet23');
var destrange = destsheet.getRange('A:D');
destrange.setValues(sourcevalues);
}
}
I would stick with the spreadsheet app versus using drive as your initial code operates on. I think you can get what you want with this below code. I included a check to ensure the row count is the same.
function getDate() {
var fromFile = SpreadsheetApp.openById("16aWONG-TyHYxYFYqfDI7Pw2PVuNvDQ0cnFoIv_SDA4U");
var targetFile = SpreadsheetApp.openById("1zIeqvQuC7RlXy9SB--SaNaEXKT399NvffM3F3nfAXGs");
var sourcesheet = fromFile.getSheetByName("BahanMix");
var sourcerange = sourcesheet.getRange('A:D');
var sourcevalues = sourcerange.getValues();
var destsheet = targetFile.getSheetByName('Sheet23');
var destrange = destsheet.getRange('A:D');
//make sure the same number or rows exist
if (destsheet.getMaxRows()!= sourcesheet.getMaxRows()){
Browser.msgBox("Row MisMatch. Destination has " +
destsheet.getMaxRows() + " rows while source has " +
sourcesheet.getMaxRows()) + " rows."
}else{
destrange.setValues(sourcevalues);
}
}
Since you are using getActive() your script is bound to your source spreadsheet, so SpreadsheetApp can be used directly to open files.
function getdata() {
var shoot = SpreadsheetApp.openById("1zIeqvQuC7RlXy9SB--SaNaEXKT399NvffM3F3nfAXGs");
var sourcesheet = SpreadsheetApp.getActive().getSheetByName("BahanMix");
var sourcerange = sourcesheet.getRange('A:D');
var sourcevalues = sourcerange.getValues();
var destsheet = shoot.getSheetByName('Sheet23');
var destrange = destsheet.getRange('A:D');
destrange.setValues(sourcevalues);
}
I am trying to copy and paste "A4:J4" and "A5:H5" but I am getting an error because A5 is merged. Also "STAGE 3" only pastes in "G4" in Sheet2. How can I copy and paste this data to Sheet2 with merged cells?
function hi()
{
var sheet = SpreadsheetApp.getActive().getSheetByName('Sheet1');
var yourRange = sheet.getRange("A4");
var ss = SpreadsheetApp.getActiveSpreadsheet();
var destSheet = ss.getSheetByName('Sheet2');
var yourLastColumn=sheet.getRange("A4:4").getLastColumn();
var range = ss.getRangeByName("A4");
var yourLastRange=sheet.getRange("A4").offset(0, yourLastColumn-1);
var rsltRange1 = yourLastRange.getNextDataCell(SpreadsheetApp.Direction.PREVIOUS);
var rsltRange2 = sheet.getRange("A4:" + rsltRange1.getA1Notation());
if (rsltRange2.isPartOfMerge()) {
var rangeTest = rsltRange2.getMergedRanges()[0];
} else {
}
Logger.log(rangeTest.getA1Notation());
var destRange = destSheet.getRange("A4");
rsltRange2.copyTo(destRange, {contentsOnly: false});
}
Here is a simplified version of your code that should copy your range correctly no matter if the range is merged or not:
function hi(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Sheet1');
var destSheet = ss.getSheetByName('Sheet2');
var startColumn=1;
var startRow=4;
var numRows=2;
var numColumns=sheet.getLastColumn()-startColumn+1;
var range=sheet.getRange(startRow, startColumn, numRows, numColumns);
var destRange = destSheet.getRange("A4");
range.copyTo(destRange);
}
I have 184 audit sheets in one folder. I want to reference one cell in each of these sheets and bring them back in to one master spreadsheet.
I have a code that does the opposite that sends a value to each sheet in the folder and changes it to the value that I want. So in essence I want to do the opposite of the script below:
function getdata() {
var files = DriveApp.getFolderById("1gbA2JI1DYNku7SQPaCq1Qk27hnbimPag").getFiles()
while (files.hasNext()) {
var file = files.next();
var shoot = SpreadsheetApp.openById(file.getId());
var sourcesheet = SpreadsheetApp.getActive().getSheetByName("Jan");
var sourcerange = sourcesheet.getRange('A3');
var sourcevalues = sourcerange.getValues();
var destsheet = shoot.getSheetByName('Front Sheet');
var destrange = destsheet.getRange('B5');
destrange.setValues(sourcevalues);
}
}
Your example code doesn't appear to match your explanation but I think I got the basic idea so I made up some of the needed details on my own. I included filenames, ids and 'A3' values. You can choose to modify as needed.
function getdata(month) {
var monthA=["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
var date=new Date();
var mss=SpreadsheetApp.getActive();
var ms=mss.getSheetByName("Master Sheet");
var vObj={name:[],id:[],value:[]};
var files = DriveApp.getFolderById("1gbA2JI1DYNku7SQPaCq1Qk27hnbimPag").getFilesByType(MimeType.GOOGLE_SHEETS);
while (files.hasNext()) {
var file = files.next();
var fid=file.getId();
var ss = SpreadsheetApp.openById(fid);
var sh = ss.getSheetByName(monthA[date.getMonth()]);
var rg = sh.getRange('A3');
vObj.name.push(file.getName());
vObj.id.push(fid);
vObj.value.push(rg.getValue());
vObj['month']=monthA[date.getMonth()];
}
vObj.id.splice(0,0,'ids');
ms.appendRow(vObj.id);
vObj.name.splice(0,0,vObj.month);
ms.appendRow(vObj.name);
vObj.value.splice(0,0,'values');
ms.appendRow(vObj.value)
}
I have a problem with the following code.
function getData() {
var files = DriveApp.getFolderById("1w86dQcDzTWC6qcCVGaEUo1tIuaaZkWEC").getFiles()
while (files.hasNext()) {
var file = files.next();
var shoot = SpreadsheetApp.openById(file.getId());
var sourceSheet = shoot.getSheets()[0];
var sourceRange = sourceSheet.getRange("C2:C");
var sourceValues = sourceRange.getValues();
var sourceRange2 = sourceSheet.getRange("D2:D");
var sourceValues2 = sourceRange2.getValues();
var destSheet = SpreadsheetApp.getActive().getSheets()[0];
var destRange = destSheet.getRange("D2:D");
destRange.setValues(sourceValues);
var destRange2 = destSheet.getRange("E2:E");
destRange2.setValues(sourceValues2);
}
}
What it does is to bring the data from only one spreadsheet and not from several, I need help to bring me the data of all the sheets without overwriting the data
Well, finally I was able to solve this little problem that I had with the method getLastRow, below I leave the code with the resolved it, I hope someone will help someday.
function getData() {
var files = DriveApp.getFolderById("1mHuYqG2aU9EFj8LE6T0DjzvZC7UK1vSj").getFiles()
while (files.hasNext()) {
var file = files.next();
var bring = SpreadsheetApp.openById(file.getId());
var sourceSheet = bring.getSheetByName("");
var n = sourceSheet.getMaxRows();
var srceRanSprint = sourceSheet.getRange(8, 2, n);
var srceValSprint = srceRanSprint.getValues();
var sourceRange2 = sourceSheet.getRange(2, 4, n);
var sourceValues2 = sourceRange2.getValues();
var destSheet = SpreadsheetApp.openById("").getSheets()[0];
var lastRow = destSheet.getLastRow() + 1;
var destRange = destSheet.getRange(lastRow, 4, n).setValues(sourceValues);
var destRange2 = destSheet.getRange(lastRow, 5, n).setValues(sourceValues2);
}
}
I'm stuck and can't find an answer for my problem.
Here's my current code:
var monthSheet = sourcesheet.getRange('A1').getValue();
var destsheet = shoot.getSheetByName(monthSheet);
monthSheet is String
destsheet is always null and I don't know why...
edit:
function sendAnnouncement() {
var files = DriveApp.getFolderById("xxxxxxxxxxxxxxxxx").getFiles()
while (files.hasNext()) {
var file = files.next();
var shoot = SpreadsheetApp.openById(file.getId());
var sourcesheet = SpreadsheetApp.getActive().getSheetByName("Announcements");
var sourceAnn = sourcesheet.getRange('B2:B2').getValue();
var sourceMonth = sourcesheet.getRange('A2').getValue();
sourcesheet.getRange('A1').setFormula("=VLOOKUP(A2;Settings!B1:C12;2;false)");
var monthSheet = sourcesheet.getRange('A1').getValue();
var sourceTaxStake = sourcesheet.getRange('C2').getValue();
var destsheet = shoot.getSheetByName(monthSheet);
var destTaxStake = destsheet.getRange('H2').getValues();
if ('sourceTaxStake' == "all")
{
var destrange = destsheet.getRange('D8:D8');
destrange.setValues(sourceAnn);
}
else if ('destTaxStake' == 'sourceTaxStake')
{
var destrange = destsheet.getRange('D8:D8');
destrange.setValues(sourceAnn);
}
else
{
}
}
}
Again, that part of code itself is okay. Have you checked what value is monthSheet? Probably it's different from the actual sheet name. Also check its value by Logger.log(monthSheet). Check my example source sheet and destination sheet. Below script is included in the source sheet.
function myFunction() {
var sourcesheet = SpreadsheetApp.getActive().getSheetByName("Announcements");
var monthSheet = sourcesheet.getRange('A1').getValue();
var shoot = SpreadsheetApp.openById('1JxjM7KJRofTRxH5pRpY4vLuY9ojKP__DYjodSIN3rVE');
var destsheet = shoot.getSheetByName(monthSheet);
Logger.log(destsheet.getName()); // dest_s
}
Of course 1JxjM7KJRofTRxH5pRpY4vLuY9ojKP__DYjodSIN3rVE is my destination sheet. And dest_s is the value of Announcements!A1 on the source sheet and, in the same time, the name of sheet on the destination sheet. Check for yourself.
I noticed that sourceTaxStake and destTaxStake had single quotes around them so neither condition would ever by true. I removed the single quotes and the editor immediately darkened them as it does with defined variables.
function sendAnnouncement()
{
var files = DriveApp.getFolderById("xxxxxxxxxxxxxxxxx").getFiles()
while (files.hasNext())
{
var file = files.next();
var shoot = SpreadsheetApp.openById(file.getId());
var sourcesheet = SpreadsheetApp.getActive().getSheetByName("Announcements");
var sourceAnn = sourcesheet.getRange('B2').getValue();
var sourceMonth = sourcesheet.getRange('A2').getValue();
sourcesheet.getRange('A1').setFormula("=VLOOKUP(A2;Settings!B1:C12;2;false)");
var monthSheet = sourcesheet.getRange('A1').getValue();
var sourceTaxStake = sourcesheet.getRange('C2').getValue();
var destsheet = shoot.getSheetByName(monthSheet);
var destTaxStake = destsheet.getRange('H2').getValues();
if (sourceTaxStake == "all")
{
var destrange = destsheet.getRange('D8:D8');
destrange.setValues(sourceAnn);
}
else if (destTaxStake == 'sourceTaxStake')
{
var destrange = destsheet.getRange('D8:D8');
destrange.setValues(sourceAnn);
}
}
}
Thanks for help. There was problem with correct name of the sheet.
I've also removed single quotes from conditions. Everything is working correctly.