Search code examples
google-apps-scriptgoogle-sheetsgoogle-sheets-formula

display the contents of the active cell to another cell?


someone asked these questions but it's not working, if possible, make corrections on the test file, please Any of between N16:N29, 16 to 29 any one cell we select that cell text or value shows on yellow N10

In Google Sheets, is it possible to display the contents of the active cell to another cell?

TEST File


Solution

  • About your request of Any of between N16:N29, 16 to 29 any one cell we select that cell text or value shows on yellow N10, how about the following sample script?

    Sample script:

    Please copy and paste the following script to the script editor of your provided Google Spreadsheet, and save the script. And, just in case, please reopen Google Spreadsheet.

    function onSelectionChange(e) {
      const sheetName = "Form"; // This is from your provided Spreadsheet.
    
      const { range } = e;
      const sheet = range.getSheet();
      const check = sheet.getSheetName() == sheetName && range.rowStart >= 16 && range.rowEnd <= 29 && range.columnStart == 14;
      const value = check ? range.getValue() : null;
      sheet.getRange("N10").setValue(value);
    }
    
    • When you want to use this script, please select one of the cells "N16:N29" of "Form" sheet. By this, the value of the selected cell is put into cell "N10".

    Testing:

    When this script is used using your provided Spreadsheet, the following result is obtained.

    enter image description here

    Note:

    • This sample script is for your provided Spreadsheet. So, please test this script using your provided Spreadsheet.

    • The function onSelectionChange is automatically run by the simple trigger when a cell is selected. I used onSelectionChange of the simple trigger from Any of between N16:N29, 16 to 29 any one cell we select that cell text or value shows on yellow N10. So, when you directly run the function onSelectionChange with the script editor, an error like TypeError: Cannot destructure property 'range' of 'e' as it is undefined. occurs, because the event object e is not given. Please be careful about this.

    Reference:

    Added:

    About your following reply,

    it's working but if I have to remove this const value = check? range.getValue() : null; Means Whenever its out of range it will keep the old value is it possible?

    In this case, how about the following sample script?

    function onSelectionChange(e) {
      const sheetName = "Form"; // This is from your provided Spreadsheet.
    
      const { range } = e;
      const sheet = range.getSheet();
      const check = sheet.getSheetName() == sheetName && range.rowStart >= 16 && range.rowEnd <= 29 && range.columnStart == 14;
      if (!check) return;
      sheet.getRange("N10").setValue(range.getValue());
    }