here's my code and i already input loop command but it only can capture the first row from spreadsheet to google form response.
i need to capture all data from spreadsheet when i execute the code.
anyone know what im missing ?
function auto_data() {
var formURL="https://docs.google.com/forms/d/e/1FAIpQLSec8kePcCeGY0vlq3FdIepQgE84Wh7DiYE1e0v7ZWvo5XhxoQ/formResponse";
var wrkBK = SpreadsheetApp.getActiveSpreadsheet();
var wrkSht= wrkBK.getSheetByName("Sheet1");
for (var i=2;i<=5;i++){
var a = wrkSht.getRange('A'+i).getDisplayValue();
var b = wrkSht.getRange('B'+i).getDisplayValue();
var c = wrkSht.getRange('C'+i).getDisplayValue();
}
var datamap = {"entry.1436037797":a,
"entry.995641845":b,
"entry.1420146463":c};
var options = {
"method":"post",
"payload": datamap };
UrlFetchApp.fetch(formURL, options);
}
move your fetch(..) call and payload composition into the for loop:
function auto_data() {
var formURL="https://docs.google.com/forms/d/e/1FAIpQLSec8kePcCeGY0vlq3FdIepQgE84Wh7DiYE1e0v7ZWvo5XhxoQ/formResponse";
var wrkBK = SpreadsheetApp.getActiveSpreadsheet();
var wrkSht= wrkBK.getSheetByName("Sheet1");
for (var i=2;i<=5;i++){
var a = wrkSht.getRange('A'+i).getDisplayValue();
var b = wrkSht.getRange('B'+i).getDisplayValue();
var c = wrkSht.getRange('C'+i).getDisplayValue();
var datamap = {"entry.1436037797":a,
"entry.995641845":b,
"entry.1420146463":c};
var options = {
"method":"post",
"payload": datamap };
UrlFetchApp.fetch(formURL, options);
}
}
Related
function saveData(){
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var formSheet = spreadsheet.getSheetByName("Receipt");
var dataSheet = spreadsheet.getSheetByName("Historical Sales")
var fieldRange = \["B12:X"\]
var fieldValue = fieldRange.map(f =\>
formSheet.getRange(f).getValue())
//console.log(fieldValue)
dataSheet.appendRow(fieldValue)
};
function exp5() {
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getRange('W12').activate();
var currentCell = spreadsheet.getCurrentCell();
spreadsheet.getSelection().getNextDataRange(SpreadsheetApp.Direction.DOWN).activate();
currentCell.activateAsCurrentCell();
currentCell = spreadsheet.getCurrentCell();
spreadsheet.getSelection().getNextDataRange(SpreadsheetApp.Direction.PREVIOUS).activate();
currentCell.activateAsCurrentCell();
spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Historical Receipts'), true);
spreadsheet.getRange('X963').activate();
spreadsheet.getCurrentCell().getNextDataCell(SpreadsheetApp.Direction.PREVIOUS).activate();
spreadsheet.getCurrentCell().getNextDataCell(SpreadsheetApp.Direction.UP).activate();
spreadsheet.getRange('A14:V23').activate();
spreadsheet.getRange('Receipt!B12:W21').copyTo(spreadsheet.getActiveRange(),
SpreadsheetApp.CopyPasteType.PASTE_VALUES, false);
};
This is how I'd do it:
//Not tested, but it can give you a direction
function moveData () {
var formSheet = spreadsheet.getSheetByName("Receipt");
var formRng = formSheet.getRange(); //Specify the data range here
var formData = formRng.getValues();
var dataSheet = spreadsheet.getSheetByName("Historical Sales");
//It gets the first empty row in column A and "pastes" the data set (form Data) .
datasheet.getRange(datasheet.getLastRow()+1,1,formData.length,formData[0].length).setValues(formData);
}
I'm relatively new to coding, so thanks in advance for any assistance here.
I have a script that runs two functions on the same sheet.
Button2 contains a function (email_button2) that runs a SQL query and pulls info into columns A to D
Button1 also contains a function (email_button1) that runs a separate query and I want to pull this info in columns G to AA (21 columns)
Right now, when I click Button1, I get the following error: Incorrect range width, was 1 but should be 21
Any idea what I should change or add to my script?
function data_button() {
// Logger.log(e)
var thisDoc = SpreadsheetApp.getActiveSpreadsheet();
var helpers = thisDoc.getSheetByName("helper");
query =
helpers.getRange(3,2).getValue().split(String.fromCharCode(13)).join(" ").split(String.fromCharCode(10)).join(" ")
// lastrow = helpers.getRange("lastrow").getValue()
do_query(query,"data")
}
function email_button1() {
// Logger.log(e)
var thisDoc = SpreadsheetApp.getActiveSpreadsheet();
var helpers = thisDoc.getSheetByName("helper");
query =
helpers.getRange(4,2).getValue().split(String.fromCharCode(13)).join("
").split(String.fromCharCode(10)).join(" ")
// lastrow = helpers.getRange("lastrow").getValue()
do_query(query,"emails",11,7)
}
function email_button2() {
// Logger.log(e)
var thisDoc = SpreadsheetApp.getActiveSpreadsheet();
var helpers = thisDoc.getSheetByName("helper");
query =
helpers.getRange(5,2).getValue().split(String.fromCharCode(13)).join("
").split(String.fromCharCode(10)).join(" ")
// lastrow = helpers.getRange("lastrow").getValue()
do_query(query,"emails",11,1)
}
function do_query(query,sheetname,startRow,startCol){
//List of lists?
var url= "xxxxxxxxxxxxxx"
var q = {"query":query}
var options = {
'method' : 'post',
'payload' : q,
'muteHttpExceptions': true
};
var response = UrlFetchApp.fetch(url, options); // get feed
var response2 = response.getContentText()
var rows = response2.split(";;;")
var columnCount = rows[0].length
var data = [];
for(var i = 0; i < rows.length; i++){
var row = rows[i].split('|||');
data.push(row);
}
Logger.log(data)
var a = data[1]
var thisDoc = SpreadsheetApp.getActiveSpreadsheet();
var sheet2 = thisDoc.getSheetByName(sheetname);
sheet2.getRange(startRow,startCol,200,4).clear()
sheet2.getRange(startRow,startCol, data.length,
data[0].length).setValues(data);
//this line above designates column headers
//sheet2.getRange(12, 1, data[1].length, data[1] .
[0].length).setValues(data[1]);
//Logger.log(data[0].length)
//Logger.log(data[0][0].length)
//Logger.log(data[0])
// thisDoc.getSheetByName("helper").getRange("lastrow").setValue(data.length)
}
I'm new to GAS. I'm trying to accomplish four tasks in a single spreadsheet with three separate sheets to relate to.
I want to copy data from the 'rawData sheet', set it in an array, rearrange the array, and set the values in the 'destination sheet'. And keep it active for new submissions (i.e. getLastRow/appendRow).
In 'destination sheet' I want to color code based on status.
Create a button in 'destination sheet' to mark row(case) complete and move from 'destination' to the 'complete sheet'.
And add onEdit triggers for each.
Your input is greatly appreciated.
Thanks.
function colorCode() {
var range = outerArray;
Logger.log(range);
var row = dBoard.getRange(dBoard.getLastRow(),1,1,dBoard.getLastColumn());
var submitter = range[3];
var status = range[11];
Logger.log(submitter);
Logger.log(status);
//var column = range.getColumn();
//var row = range.getRow();
if (status === "Approved"){
var rowToColor = row.offset(0,0,1,13);
rowToColor.setBackground("#6bb2ff");
}
else if (status === "Requester"){
var rowToColor = row.offset(0,0,1,13);
rowToColor.setBackground("#ffe16b");
}
else if (range === 12 && range > 1 && status === "Denied"){
var rowToColor = row.offset(0,0,1,13);
rowToColor.setBackground("#f9441b");
}
}
var ss = SpreadsheetApp.getActiveSpreadsheet();
var source1 = ss.getSheetByName("Copy of rawData");
var dBoard = ss.getSheetByName("dBoard2");
//function copyData() {
//var rawD = source1.getRange(1,1,source1.getLastRow(),source1.getLastColumn()).getValues();
//Logger.log(rawD);
function setData() {
var rawD = source1.getRange(source1.getLastRow(),1,1,source1.getLastColumn()).getValues();
//Logger.log(limits);
var columnA = rawD[0][0];
var columnB = rawD[0][18];
var columnC = rawD[0][13];
var columnD = rawD[0][2];
var columnE = rawD[0][3]
var columnF = rawD[0][4];
var columnG = rawD[0][5];
var columnH = rawD[0][6];
var columnI = rawD[0][7];
var columnJ = rawD[0][8];
var columnK = rawD[0][9];
var columnL = rawD[0][11];
var columnM = rawD[0][12];
var outerArray = [columnA,
columnB,
columnC,
columnD,
columnE,
columnF,
columnG,
columnH,
columnI,
columnJ,
columnK,
columnL,
columnM];
Logger.log(columnA);
Logger.log(columnB);
Logger.log(outerArray);
dBoard.getRange(2,1,outerArray.length,outerArray.length);
dBoard.appendRow(outerArray);
}
I'm working on a simple google script for a spreadsheet that will go down a column in the sheet and update the column next to it based on information pulled from links in the original column.
The problem is that the same output is being output regardless of the input column. My code is here:
function idThatSucka() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var range = ss.getRange("F2:F20");
for (var y = 1; y <= range.getHeight(); y++) {
var logCell = ss.getRange("E"+y+":E"+y);
var url = ss.getRange("F"+y+":F"+y).getValue();
if (url){
var response = UrlFetchApp.fetch(url);
Logger.log(response.getContentText());
var theLog = Logger.getLog();
var start = theLog.split('</title>')[0];
var output = start.split('Steam Community :: ')[1];
logCell.setValue(output);
}
}
}
I suspect that "url" might not be updating, but nothing I've tried so far has fixed the problem.
Try like this
function idThatSucka() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var range = ss.getRange("F2:F20");
for (var y = 1; y <= range.getHeight(); y++) {
var url='';
var logCell='';
logCell = ss.getRange("E"+y+":E"+y);
url = ss.getRange("F"+y+":F"+y).getValue();
if (url){
var response = UrlFetchApp.fetch(url);
Logger.log(response.getContentText());
var theLog = Logger.getLog();
var start = theLog.split('</title>')[0];
var output = start.split('Steam Community :: ')[1];
logCell.setValue(output);
}
}
}
I have looked and looked and tried to adapt some scripts but am stuck.
Can someone help/show me how to add a collection of specific cells on one sheet and populate the next available row on a second sheet with these values? Please. This is what I have:
function AddToDB2() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var target = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = ss.getSheetByName("PurchaseOrder");
var targetSheet = target.getSheetByName("DataBase");
var range = ss.getDataRange();
var values = range.getValues();
var date1 = values[1][6]; //these values are collected from multiple cells not in a specific row or col
var po = values[2][6];
var vendor = values[7][0];
var shipto = values[7][3];
//this was for testing that the values were grabbed correctly.
var data = [date1,po,vendor,shipto];
// I don't think this part is even close
var lastRow = targetSheet.getLastRow()
var rowData = values[0]
var newData = []
for(n=1;n<lastRow;++n){newData.push(rowData)}
targetSheet.getRange("C2:H"+lastRow).setValues(newData);
}
This is the other way I came up with:
function addToDB() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var target = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = ss.getSheetByName("PurchaseOrder");
var targetSheet = target.getSheetByName("DataBase");
var sourceRangePO = sourceSheet.getRange("G3");
var poValues = sourceRangePO.getValues();
if(poValues != "") {
var sourceRangeDate = sourceSheet.getRange("G2");
var targetRangeDate = targetSheet.getRange("A2");
var dateValues = sourceRangeDate.getValues();
targetRangeDate.setValues(dateValues);
var sourceRangePO = sourceSheet.getRange("G3");
var targetRangePO = targetSheet.getRange("B2");
var poValues = sourceRangePO.getValues();
targetRangePO.setValues(poValues);
var sourceRangeVendor = sourceSheet.getRange("A8");
var targetRangeVendor = targetSheet.getRange("C2");
var vendorValues = sourceRangeVendor.getValues();
targetRangeVendor.setValues(vendorValues);
var sourceRangeShipTo = sourceSheet.getRange("D8");
var targetRangeShipTo = targetSheet.getRange("D2");
var shipToValues = sourceRangeShipTo.getValues();
targetRangeShipTo.setValues(shipToValues);
targetSheet.insertRowAfter(1);
Browser.msgBox("Your PO has beed saved!");
}
else { Browser.msgBox("Please enter a PO number"); }
}
You were very close. Try this. I was a little confused by your setValues C:H. You only have 4 values to set. I set them in D-G. You can adjust as needed.
function AddToDB2() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sourceSheet = ss.getSheetByName("PurchaseOrder");
var targetSheet = ss.getSheetByName("DataBase");
var range = sourceSheet.getDataRange();
var values = range.getValues();
var date1 = values[1][6]; //these values are collected from multiple cells not in a specific row or col
var po = values[2][6];
var vendor = values[7][0];
var shipto = values[7][3];
var data = [date1,po,vendor,shipto];
var lastRow = targetSheet.getLastRow()
var newData = []
for(n=0;n<data.length;++n){newData.push([data[n]])}
var rng =targetSheet.getRange(lastRow+1,1,1,4) // Col A-D
rng.setValues([newData]);
}