I am trying to copy one Google Sheet Data to Other Google Sheet after first available free row.
function CopyDataToNewFile() {
var sss = SpreadsheetApp.openById('1vHc........'); // sss = source spreadsheet
var ss = sss.getSheetByName('Database'); // ss = source sheet
//Get full range of data
var SRange = ss.getDataRange();
//get A1 notation identifying the range
var A1Range = SRange.getA1Notation();
//get the data values in range
var SData = SRange.getValues();
var tss = SpreadsheetApp.openById('1ss......'); // tss = target spreadsheet
var ts = tss.getSheetByName('CalDatabase'); // ts = target sheet
ts.getRange(A1Range).setValues(SData);
}
Above quote works fine but always Copy into same Row, but I want it to copy the data into a new blank row available below.
Try this -
function CopyDataToNewFile() {
var sss = SpreadsheetApp.openById('1vHc........'); // sss = source spreadsheet
var ss = sss.getSheetByName('Database'); // ss = source sheet
var SData = ss.getDataRange().getValues();
var tss = SpreadsheetApp.openById('1ss......'); // tss = target spreadsheet
var ts = tss.getSheetByName('CalDatabase'); // ts = target sheet
ts.getRange(ts.getLastRow()+1,1,SData.length,SData[0].length).setValues(SData);
}
Related
I would like to remove the values after the first comma. It is possible that there will be more values and commas.
Expected result:
8-1
10-1
2-5
5-8
ss from Logger.log for var Id
function myFunction() {
var Sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
var FirstRow = 7;
var LastRow = Sheet.getLastRow();
var RowRange = LastRow - FirstRow + 1;
var WholeRange = Sheet.getRange(FirstRow,3,RowRange,9);
var AllValues = WholeRange.getValues();
for (var i=0;i<AllValues.length;i++){
var CurrentRow = AllValues[i];
var Id = CurrentRow[0]; //col with ID Sheet1
var firstId = Id.map(vA=>[vA.split(',')[1]]);
}
}
Explanation:
You can use the split method and map to apply this operation for all the input data and get the first [0] element of the resulting array:
data.map(vA=>[vA.split(',')[0]])
Code snippet:
function myFunction() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName('Sheet1');
const avals = sh.getRange('A1:A4').getValues().flat();
const bvals = avals.map(vA=>[vA.split(',')[0]]);
sh.getRange(1,2,bvals.length,1).setValues(bvals);
}
Sheet used for code snippet:
Make sure you format the input text as plain text, otherwise google sheets consider the input data to be dates or alternatively use getDisplayValues() instead of getValues() but I guess you have already figured out that part because console.log returns the correct input.
Updated answer based on your edited question:
Modify your current for loop as follows:
for (var i=0;i<AllValues.length;i++){
var CurrentRow = AllValues[i];
var Id = CurrentRow[0]; //col with ID Sheet1
var newId = Id.split(',')[0];
console.log(newId);
}
Link of My sheet is :
https://docs.google.com/spreadsheets/d/1czJbRU5ELNft1IfGq1cABGe30j8BWjnffVCEa8A_AeY/edit?usp=sharing
I am trying to move data if N is equal to today. I have set the trigger. This script runs on time driven between 8 PM to 9 PM. It copies the data in Row 8 when column K onwards there is noting mentioned. In the current Payment Approval Sheet, while running the script it copies the data in 1500th row.
The script I am using is as below:
function copyrange() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Complete Invoice Sheet'); //source sheet
var testrange = sheet.getRange('N:N');
var testvalue = (testrange.setNumberFormat("#").getValues());
var ds = ss.getSheetByName('Payment Approval Sheet'); //destination sheet
var data = [];
var j =[];
var dt = new Date();
var today = Utilities.formatDate(new Date(), 'GMT-0', 'dd/MM/yyyy')
//Condition to check in N:N, if true, copy the same row to data array
for (i=0;i<testvalue.length;i++) {
if (testvalue[i] == today) {
data.push.apply(data,sheet.getRange(i+1,1,1,13).getValues());
//Copy matched ROW numbers to j
j.push(i);
}
}
//Copy data array to destination sheet
ds.getRange(ds.getLastRow()+1,1,data.length,data[0].length).setValues(data);
}
Issue:
Your current solution considers the last row of your destination
sheet Payment Approval Sheet. However, in that sheet, checkboxes
are populated in column N until the bottom of the sheet. Therefore,
getLastRow() returns the row at the bottom of column N which is not
what you want.
Explanation:
Instead of using getLastRow(), calculate the number of elements after cell A7 by using the filter() operation and then use this as a starting point when you copy & paste the data to the destination sheet:
var start_row=ds.getRange('A8:A').getValues().filter(String).length +7; //calculate max row
ds.getRange(start_row+1,1,data.length,data[0].length).setValues(data);
Solution:
function copyrange() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Complete Invoice Sheet'); //source sheet
var testrange = sheet.getRange('K:K');
var testvalue = (testrange.setNumberFormat("#").getValues());
Logger.log(testvalue);
var ds = ss.getSheetByName('Payment Approval Sheet'); //destination sheet
var data = [];
var j =[];
var dt = new Date();
var today = Utilities.formatDate(new Date(), 'GMT-0', 'dd/MM/yyyy')
//Condition to check in N:N, if true, copy the same row to data array
for (i=0;i<testvalue.length;i++) {
if (testvalue[i] == today) {
data.push.apply(data,sheet.getRange(i+1,1,1,13).getValues());
//Copy matched ROW numbers to j
j.push(i);
}
}
//Copy data array to destination sheet
var start_row=ds.getRange('A8:A').getValues().filter(String).length +7; //calculate max row
ds.getRange(start_row+1,1,data.length,data[0].length).setValues(data);
}
I have two functions in a google sheet that are meant to loop through a single column containing the file IDs of files in google drive, issuing a Properties.get to retrieve a single property "LastReview" for each document and paste all of the LastReview times in the next available column.
I'm having trouble getting the loop in "loopForMetadata" to work. I want it to acquire a list of all the LastReview times associated with each fileID and then post that to the next available column so that all of the LastReview times align with the fileIDs.
function getProperty(fileId) {
var propertyKey = 'LastReview'
var fileId = '1UaQkJU8r1kE9sxpFg6OD8aOuUCoRnSpB9Agg_R9HJ3s'
var response = JSON.stringify(Drive.Properties.get(fileId, 'LastReview', { visibility: 'PUBLIC' }).value);
var key = "value";
var resposeNoQuote = response.replace(/\"/g, "")
Logger.log(resposeNoQuote);
}
function loopForMetadata() {
var columnNeeded, data, lastColumn, sh;
sh = SpreadsheetApp.getActiveSheet();
lastColumn = sh.getLastColumn();
data = sh.getRange(1, 1, 1, lastColumn).getValues();//Get 2D array of all values in row one
data = data[0];//Get the first and only inner array
columnNeeded = data.indexOf('ID') + 1;//Arrays are zero indexed- add 1
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var rangeData = sheet.getDataRange();
var lastColumn = rangeData.getLastColumn();
var lastRow = rangeData.getLastRow();
var searchRange = sheet.getRange(2, columnNeeded, lastRow - 1, 1);
// Get array of values in the search Range
var rangeValues = searchRange.getValues();
// Loop through array and if condition met, add relevant
// background color.
var resultValues = []
for (i = 0; i < rangeValues.length; i++) {
resultValues.push(getProperty(rangeValues[i]));
Utilities.sleep(10);
}
Logger.log(resultValues);
};
I believe your goal as situation as follows.
You want to retrieve the values using the key of LastReview in the property from the files of fileId.
You want to put the retrieved values to the same row of fileId in the next available column.
You want to achieve this using Google Apps Script.
Modification point:
In your script,
getProperty() doesn't return the values.
resultValues is not put to the Spreadsheet.
When you want to retrieve the value of the key LastReview, Drive.Properties.get(fileId, 'LastReview', { visibility: 'PUBLIC' }).value directly returns the value.
As an important point, when the file of fileId has not property of the key LastReview, it seems that Drive.Properties.get() occurs an error. So in this modification, as a simple workaround, I used the try catch.
When above points are reflected to your script, it becomes as follows.
Sample script:
function loopForMetadata() {
var columnNeeded, data, lastColumn, sh;
sh = SpreadsheetApp.getActiveSheet();
lastColumn = sh.getLastColumn();
data = sh.getRange(1, 1, 1, lastColumn).getValues();//Get 2D array of all values in row one
data = data[0];//Get the first and only inner array
columnNeeded = data.indexOf('ID') + 1;//Arrays are zero indexed- add 1
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var rangeData = sheet.getDataRange();
var lastColumn = rangeData.getLastColumn();
var lastRow = rangeData.getLastRow();
var searchRange = sheet.getRange(2, columnNeeded, lastRow - 1, 1);
var rangeValues = searchRange.getValues();
var resultValues = []
// I modified below script.
for (i = 0; i < rangeValues.length; i++) {
var fileId = rangeValues[i];
var value = "";
try {
value = Drive.Properties.get(fileId, 'LastReview', { visibility: 'PUBLIC' }).value;
} catch(e) {}
resultValues.push([value]);
// Utilities.sleep(10); // I'm not sure whether this is required.
}
sheet.getRange(2, lastColumn + 1, resultValues.length, 1).setValues(resultValues);
Logger.log(resultValues);
};
Note:
Please confirm whether Drive API is enabled at Advanced Google services, again.
Reference:
Properties: get
This script fetches the data from the sheet and insert it into a new sheet. I want the second function to insert into the third row each time.
I want the result to be -
Timestamp | Impressions | CTR
function AggregateData() {
Impressions();
CTR();
}
function Impressions() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Metrics");
var source = sheet.getRange("G16:H16");
var values = source.getValues();
var sheet = ss.getSheetByName("Aggregate");
values[0][0] = new Date();
sheet.appendRow(values[0]);
};
function CTR() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Metrics");
var source = sheet.getRange("F16:G16");
var values = source.getValues();
var sheet = ss.getSheetByName("Aggregate");
values[0][0] = new Date();
sheet.appendRow(values[0]);
};
This code appends the second function in the second column itself below the result of the first function.
This code will copy your data to a new sheet in the order you want. However, I have reduced it to one function. The reason your code puts the new data in a new row is this line:
Sheet.appendRow(data)
puts the data you supply to a new row which is empty. That is also the reason why i have combined the two functions into one.
function AggregateData() {
Impressions();
}
function Impressions() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Metrics");
var source = sheet.getRange("F16:H16");
var values = source.getValues();
var pasteValues = [] // create new array which will hold the data you need to be pasted into the aggregate Sheet
pasteValues[0] = [] // Adding a second dimesion to the array
var sheet = ss.getSheetByName("Aggregate");
// below 3 lines does the switch of data and adds the time stamp
pasteValues[0][0] = new Date();
pasteValues[0][1] = values[0][2] //Copy the col h (impressions value) value into the new array
pasteValues[0][2] = values[0][1] //Copy the col G (impressions value) value into the new array
sheet.appendRow(pasteValues[0]); //Append the new array into the sheet
};
I need a help reading cell value using either java or google script. The cell I am trying to read has a formula. When my script reads the data I get #DIV\0! error.
here is my simple script:
function readData(){
var Keys = "1Vp_fjFVFXHDjJRXl7C6mNzCK66jVB1I1BjieaZK6P";
var SheetName = "AAL";
var WR;
SS = SpreadsheetApp.openById(Keys);
Sheet = SS.getSheetByName(SheetName);
Range = Sheet.getDataRange();
Data = Range.getValues();
WR = Data[30][15];
}
Any help will be appriciated, Thanks.
Instead of doing [30][15], use this code to limit the range to one cell.
If that doesn't work, maybe your cell actually contains this DIV/0 value?
function readData(){
var Keys = "1Vp_fjFVFXHDjJRXl7C6mNzCK66jVB1I1BjieaZK6P";
var SheetName = "AAL";
var WR;
SS = SpreadsheetApp.openById(Keys);
Sheet = SS.getSheetByName(SheetName);
Range = Sheet.getRange(30, 15); //changed here
Data = Range.getValue(); //and here
WR = Data; //and here
}