Google Sheet Apps Script - Change Cell Data and Increment One Row Down
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I'm trying to.. 1) replace a substring from the contents of the active cell in my sheet, and.. 2) move the active cell down one spot, and repeat until the active cell value is empty. Here's my function, but nothing happens when I run it...can anyone see why?
var app = SpreadsheetApp;
var mySheet = ss.getSheetByName('NAME');
var activeCell = mySheet.getActiveCell();
var cellValue = activeCell.getValue();
var activeRow = activeCell.getRow();
function replaceStringInCell(){
while(cellValue != ''){
var newCellValue = cellValue.replace('MC/Visa/Discover', 'CC');
activeCell.setValue(newCellValue);
activeRow++;
mySheet.getRange(activeRow, 7).activate();
}
}
google-apps-script
add a comment |
I'm trying to.. 1) replace a substring from the contents of the active cell in my sheet, and.. 2) move the active cell down one spot, and repeat until the active cell value is empty. Here's my function, but nothing happens when I run it...can anyone see why?
var app = SpreadsheetApp;
var mySheet = ss.getSheetByName('NAME');
var activeCell = mySheet.getActiveCell();
var cellValue = activeCell.getValue();
var activeRow = activeCell.getRow();
function replaceStringInCell(){
while(cellValue != ''){
var newCellValue = cellValue.replace('MC/Visa/Discover', 'CC');
activeCell.setValue(newCellValue);
activeRow++;
mySheet.getRange(activeRow, 7).activate();
}
}
google-apps-script
Thank you...I tried adding the SpreadsheetApp.flush(), and this made the cell move down consistently, but only the first cell had its content modified. It's as if the loop can only execute one command maximum..
– Silvia Biurrun
Nov 17 '18 at 16:55
It's because the active cell isn't changed in the loop and only refers to the initial cell. Do you really want visual feedback?- Why can't you just change all of the column in one batch?
– TheMaster
Nov 17 '18 at 23:40
Sorry, I'm not sure what you mean by that. Could you provide an example?
– Silvia Biurrun
Nov 18 '18 at 1:07
Yes, that would work, but I'm just replacing a substring in each cell if it's contained therein.
– Silvia Biurrun
Nov 19 '18 at 2:28
add a comment |
I'm trying to.. 1) replace a substring from the contents of the active cell in my sheet, and.. 2) move the active cell down one spot, and repeat until the active cell value is empty. Here's my function, but nothing happens when I run it...can anyone see why?
var app = SpreadsheetApp;
var mySheet = ss.getSheetByName('NAME');
var activeCell = mySheet.getActiveCell();
var cellValue = activeCell.getValue();
var activeRow = activeCell.getRow();
function replaceStringInCell(){
while(cellValue != ''){
var newCellValue = cellValue.replace('MC/Visa/Discover', 'CC');
activeCell.setValue(newCellValue);
activeRow++;
mySheet.getRange(activeRow, 7).activate();
}
}
google-apps-script
I'm trying to.. 1) replace a substring from the contents of the active cell in my sheet, and.. 2) move the active cell down one spot, and repeat until the active cell value is empty. Here's my function, but nothing happens when I run it...can anyone see why?
var app = SpreadsheetApp;
var mySheet = ss.getSheetByName('NAME');
var activeCell = mySheet.getActiveCell();
var cellValue = activeCell.getValue();
var activeRow = activeCell.getRow();
function replaceStringInCell(){
while(cellValue != ''){
var newCellValue = cellValue.replace('MC/Visa/Discover', 'CC');
activeCell.setValue(newCellValue);
activeRow++;
mySheet.getRange(activeRow, 7).activate();
}
}
google-apps-script
google-apps-script
edited Nov 17 '18 at 5:36
Silvia Biurrun
asked Nov 17 '18 at 3:01
Silvia BiurrunSilvia Biurrun
11
11
Thank you...I tried adding the SpreadsheetApp.flush(), and this made the cell move down consistently, but only the first cell had its content modified. It's as if the loop can only execute one command maximum..
– Silvia Biurrun
Nov 17 '18 at 16:55
It's because the active cell isn't changed in the loop and only refers to the initial cell. Do you really want visual feedback?- Why can't you just change all of the column in one batch?
– TheMaster
Nov 17 '18 at 23:40
Sorry, I'm not sure what you mean by that. Could you provide an example?
– Silvia Biurrun
Nov 18 '18 at 1:07
Yes, that would work, but I'm just replacing a substring in each cell if it's contained therein.
– Silvia Biurrun
Nov 19 '18 at 2:28
add a comment |
Thank you...I tried adding the SpreadsheetApp.flush(), and this made the cell move down consistently, but only the first cell had its content modified. It's as if the loop can only execute one command maximum..
– Silvia Biurrun
Nov 17 '18 at 16:55
It's because the active cell isn't changed in the loop and only refers to the initial cell. Do you really want visual feedback?- Why can't you just change all of the column in one batch?
– TheMaster
Nov 17 '18 at 23:40
Sorry, I'm not sure what you mean by that. Could you provide an example?
– Silvia Biurrun
Nov 18 '18 at 1:07
Yes, that would work, but I'm just replacing a substring in each cell if it's contained therein.
– Silvia Biurrun
Nov 19 '18 at 2:28
Thank you...I tried adding the SpreadsheetApp.flush(), and this made the cell move down consistently, but only the first cell had its content modified. It's as if the loop can only execute one command maximum..
– Silvia Biurrun
Nov 17 '18 at 16:55
Thank you...I tried adding the SpreadsheetApp.flush(), and this made the cell move down consistently, but only the first cell had its content modified. It's as if the loop can only execute one command maximum..
– Silvia Biurrun
Nov 17 '18 at 16:55
It's because the active cell isn't changed in the loop and only refers to the initial cell. Do you really want visual feedback?- Why can't you just change all of the column in one batch?
– TheMaster
Nov 17 '18 at 23:40
It's because the active cell isn't changed in the loop and only refers to the initial cell. Do you really want visual feedback?- Why can't you just change all of the column in one batch?
– TheMaster
Nov 17 '18 at 23:40
Sorry, I'm not sure what you mean by that. Could you provide an example?
– Silvia Biurrun
Nov 18 '18 at 1:07
Sorry, I'm not sure what you mean by that. Could you provide an example?
– Silvia Biurrun
Nov 18 '18 at 1:07
Yes, that would work, but I'm just replacing a substring in each cell if it's contained therein.
– Silvia Biurrun
Nov 19 '18 at 2:28
Yes, that would work, but I'm just replacing a substring in each cell if it's contained therein.
– Silvia Biurrun
Nov 19 '18 at 2:28
add a comment |
1 Answer
1
active
oldest
votes
Issue:
- Unnecessary looping over each cell
Solution:
- Use batch operations
Sample Script:
function replaceColA() {
var ss = SpreadsheetApp.getActive();
var mySheet = ss.getSheetByName('NAME');
var cellRange = mySheet.getRange('A1:A' + mySheet.getLastRow());
var cellValues = cellRange.getValues().map(replaceStringInCell); //call replace function on all values in range
cellRange.setValues(cellValues); //set mapped values back to range
}
function replaceStringInCell(cellValue) {
if (cellValue.map) {
//if cellValue is a array
return cellValue.map(replaceStringInCell); //recurse
} else {
return cellValue.replace('MC/Visa/Discover', 'CC');
}
}
To Read:
Arrays
Array#Methods
Array#2DFromSpreadsheets
Array#map
String#replace
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53347811%2fgoogle-sheet-apps-script-change-cell-data-and-increment-one-row-down%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Issue:
- Unnecessary looping over each cell
Solution:
- Use batch operations
Sample Script:
function replaceColA() {
var ss = SpreadsheetApp.getActive();
var mySheet = ss.getSheetByName('NAME');
var cellRange = mySheet.getRange('A1:A' + mySheet.getLastRow());
var cellValues = cellRange.getValues().map(replaceStringInCell); //call replace function on all values in range
cellRange.setValues(cellValues); //set mapped values back to range
}
function replaceStringInCell(cellValue) {
if (cellValue.map) {
//if cellValue is a array
return cellValue.map(replaceStringInCell); //recurse
} else {
return cellValue.replace('MC/Visa/Discover', 'CC');
}
}
To Read:
Arrays
Array#Methods
Array#2DFromSpreadsheets
Array#map
String#replace
add a comment |
Issue:
- Unnecessary looping over each cell
Solution:
- Use batch operations
Sample Script:
function replaceColA() {
var ss = SpreadsheetApp.getActive();
var mySheet = ss.getSheetByName('NAME');
var cellRange = mySheet.getRange('A1:A' + mySheet.getLastRow());
var cellValues = cellRange.getValues().map(replaceStringInCell); //call replace function on all values in range
cellRange.setValues(cellValues); //set mapped values back to range
}
function replaceStringInCell(cellValue) {
if (cellValue.map) {
//if cellValue is a array
return cellValue.map(replaceStringInCell); //recurse
} else {
return cellValue.replace('MC/Visa/Discover', 'CC');
}
}
To Read:
Arrays
Array#Methods
Array#2DFromSpreadsheets
Array#map
String#replace
add a comment |
Issue:
- Unnecessary looping over each cell
Solution:
- Use batch operations
Sample Script:
function replaceColA() {
var ss = SpreadsheetApp.getActive();
var mySheet = ss.getSheetByName('NAME');
var cellRange = mySheet.getRange('A1:A' + mySheet.getLastRow());
var cellValues = cellRange.getValues().map(replaceStringInCell); //call replace function on all values in range
cellRange.setValues(cellValues); //set mapped values back to range
}
function replaceStringInCell(cellValue) {
if (cellValue.map) {
//if cellValue is a array
return cellValue.map(replaceStringInCell); //recurse
} else {
return cellValue.replace('MC/Visa/Discover', 'CC');
}
}
To Read:
Arrays
Array#Methods
Array#2DFromSpreadsheets
Array#map
String#replace
Issue:
- Unnecessary looping over each cell
Solution:
- Use batch operations
Sample Script:
function replaceColA() {
var ss = SpreadsheetApp.getActive();
var mySheet = ss.getSheetByName('NAME');
var cellRange = mySheet.getRange('A1:A' + mySheet.getLastRow());
var cellValues = cellRange.getValues().map(replaceStringInCell); //call replace function on all values in range
cellRange.setValues(cellValues); //set mapped values back to range
}
function replaceStringInCell(cellValue) {
if (cellValue.map) {
//if cellValue is a array
return cellValue.map(replaceStringInCell); //recurse
} else {
return cellValue.replace('MC/Visa/Discover', 'CC');
}
}
To Read:
Arrays
Array#Methods
Array#2DFromSpreadsheets
Array#map
String#replace
answered Nov 19 '18 at 14:38
TheMasterTheMaster
10.9k3937
10.9k3937
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53347811%2fgoogle-sheet-apps-script-change-cell-data-and-increment-one-row-down%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Thank you...I tried adding the SpreadsheetApp.flush(), and this made the cell move down consistently, but only the first cell had its content modified. It's as if the loop can only execute one command maximum..
– Silvia Biurrun
Nov 17 '18 at 16:55
It's because the active cell isn't changed in the loop and only refers to the initial cell. Do you really want visual feedback?- Why can't you just change all of the column in one batch?
– TheMaster
Nov 17 '18 at 23:40
Sorry, I'm not sure what you mean by that. Could you provide an example?
– Silvia Biurrun
Nov 18 '18 at 1:07
Yes, that would work, but I'm just replacing a substring in each cell if it's contained therein.
– Silvia Biurrun
Nov 19 '18 at 2:28