分类 技术 下的文章

  1. 开启宏
  2. 创建 sheet 级别的 vb 脚本
  3. 将下面代码写入
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim watchRange As Range
    Dim newVal As String, oldVal As String
    Dim items() As String
    Dim i As Long, result As String

    ' 多选所在列:根据模板实际列号调整
    Set watchRange = Intersect(Target, Me.Range("D:D"))
    If watchRange Is Nothing Or Target.CountLarge > 1 Then Exit Sub

    On Error GoTo ExitHandler
    Application.EnableEvents = False

    newVal = Target.Value          ' 用户当前输入/选择
    Application.Undo
    oldVal = Target.Value          ' 原来的值(逗号分隔)

    ' 用户手动清空:直接置空返回
    If Len(newVal) = 0 Then
        Target.Value = ""
        GoTo ExitHandler
    End If

    ' 原来没有任何内容:直接写入新值
    If Len(oldVal) = 0 Then
        Target.Value = newVal
        GoTo ExitHandler
    End If

    ' 拆分旧值,去掉重复项
    items = Split(oldVal, ",")
    result = ""
    For i = LBound(items) To UBound(items)
        items(i) = Trim$(items(i))
        If Len(items(i)) > 0 _
           And StrComp(items(i), newVal, vbTextCompare) <> 0 _
           And InStr(1, "," & result & ",", "," & items(i) & ",", vbTextCompare) = 0 Then
            result = result & IIf(Len(result) = 0, "", ",") & items(i)
        End If
    Next i

    ' 若新值不在旧值里,则追加;若已存在,相当于反选,直接不追加
    If InStr(1, "," & oldVal & ",", "," & newVal & ",", vbTextCompare) = 0 Then
        result = result & IIf(Len(result) = 0, "", ",") & newVal
    End If

    Target.Value = result

ExitHandler:
    Application.EnableEvents = True
End Sub

这个脚本可以实现 选择,清除,反选 操作

# 定义环境变量的作用域: "User" (用户级) 或 "Machine" (系统级).
# 设置为 "Machine" 需要以管理员身份运行 PowerShell.
$scope = "User" 

Write-Host "Setting permanent environment variables for scope: $scope"
Write-Host "IMPORTANT: These variables will be available in NEW PowerShell or CMD windows." -ForegroundColor Yellow
Write-Host ""

# 这是一个辅助函数,用于设置永久变量并打印状态
function Set-PermanentEnvVar {
    param (
        [string]$Name,
        [string]$Value,
        [string]$TargetScope
    )
    try {
        # 1. 永久设置环境变量
        [System.Environment]::SetEnvironmentVariable($Name, $Value, $TargetScope)
        
        # 2. 【已修正】为当前会话也设置该变量,以便立即生效
        Set-Item -Path "env:$Name" -Value $Value
        
        Write-Host "[SUCCESS] Set '$Name'" -ForegroundColor Green
    }
    catch {
        Write-Host "[FAILED]  Could not set '$Name'. Run as Administrator if scope is 'Machine'." -ForegroundColor Red
    }
}

# --- 需要设置的变量列表 ---

Set-PermanentEnvVar "POSTGRES_HOST" "localhost" $scope
Set-PermanentEnvVar "POSTGRES_PORT" "35432" $scope
Set-PermanentEnvVar "POSTGRES_USERNAME" "admin" $scope
Set-PermanentEnvVar "POSTGRES_PASSWORD" "123qwe." $scope

Write-Host ""
Write-Host "All variables have been set. They are also available in this current session." -ForegroundColor Cyan

我使用的是 docker-compose 来部署环境,其他方式的 docker 部署都是一样的

先准备一个目录

mysql-init-demo/
├── docker-compose.yml   # Docker Compose 配置文件
└── init/                  # 存放初始化脚本的文件夹
    └── init.sql         # 我们的 SQL 初始化脚本

在 init.sql 写上你需要导入的 sql 内容,记得没有库你需要创建先

-- 创建数据库
CREATE DATABASE IF NOT EXISTS `xxxx` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
-- 使用数据库
USE `xxxx`;

-- 在下面的都是你自己的 sql 内容

在 docker-compose.yml 中关于 mysql 内容块中加入下面的内容

services:
  # ----------------------------------------------------------------
  # MYSQL
  # ----------------------------------------------------------------
  mysql-init-sql:
    container_name: mysql-init-sql
    volumes:
      - ./init-sql:/docker-entrypoint-initdb.d
... 其他的都是你自己的文件内容

这下面的内容需要看

  • 初始化脚本只在数据库第一次创建时执行。如果你想重新执行初始化,必须彻底删除容器和它关联的数据卷。(如果已经有其他数据库了记得备份啊)
  • 如果 /docker-entrypoint-initdb.d 目录下有多个文件,它们会按 字母顺序 执行。你可以使用 01-schema.sql, 02-data.sql 这样的命名方式来控制执行顺序。
  • 除了 .sql 文件,你还可以放入 .sh 脚本文件。例如,你可以用 shell 脚本来执行 mysql 命令导入一个大的数据文件,或者做一些更复杂的逻辑判断。